1.1.0 • Published 3 years ago

awesome-pubsub-js v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

awesome-pubsub-js

NPM JavaScript Style Guide

JavaScript implementation of the Publish/Subscribe pattern with TypeScript support.

Install

npm

$ npm install awesome-pubsub-js

yarn

$ yarn add awesome-pubsub-js

A quick example

Do you want to know more? Go to the Documentation section

JavaScript:
import PubSub from "awesome-pubsub-js";

const pubSub = PubSub();

pubSub.subscribe("event.example", (data) => {
    // data = { name: "John", email: "john@gmail.com" }
    // ... your logic
});

pubSub.publish("event.example", { name: "John", email: "john@gmail.com" });

API

List of all available methods:

Each of the following methods takes one or two arguments

  • Subscribe
Method namePayloadReturn value
subscribepubSub.subscribe("eventName", (data) => {});HashKey (string) - is needed for use in the 'unsubscribe' method
unsubscribepubSub.unsubscribe('hashKey') hashKey (string) is always returned from the subscribe methodtrue - when the event has been successfully unsubscribed false - when the event does not exists
publishpubSub.publish('eventName', any) any = literally anything you want to pass :) If you don't pass anything, the default value will be undefinedtrue - when the event has published successfully false - when the event has not been published (e.g. due to the lack of a registered subscriber)
getAllSubscribers-returns the current subscription list

Roadmap:

  • subscribe and publish method
  • unsubscribe method
  • getAllSubscribers method
  • Wildcard support
  • Logger