1.0.2 • Published 3 years ago

pubsub-simplified-js v1.0.2

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

pubsub-simplified-js

Dependency free pubsub model based library

Importing the library

const pubsub = require('pubsub-simplified-js');

Subscribing to an event

const eventFun1 = data => console.log('do something with the data'); const eventFun2 = data => console.log('do something with the data here also'); const subscriber1st = pubsub.subscribe('eventName1', eventFun1); const subscriber2nd = pubsub.subscribe('eventName1', eventFun2); const subscriber3rd = pubsub.subscribe('eventName2', eventFun1);

Publishing an event

pubsub.publish('eventName1', payloadRequiredByCallbackHere'); returns: nothing

Unsubscribe from previously subscribed event (for any particular callback for any event)

subscriber1st.unsubscribe(); returns: nothing

Remove all events and all callbacks associated with events

pubsub.removeAll() returns: nothing

Remove all callbacks for an event and event also

pubsub.remove('eventName1') returns: nothing

Detach any callack from all events

pubsubClass.detachFromAllEvents(eventFun1) returns: 1. true -> if such function is associated with any event and detached successfully 2. false -> if no such function is associated with any event

Get all subscribers for a particular event

pubsubClass.getSubscribers(eventName1) returns: 1. [] -> if no sch event is there (any event can't exist without <= 1 callback associated with it) 2. cb1, cb2, ...... -> all callbacks associated

Get all subscribers count for a particular event

pubsubClass.getSubscribersCount(eventName1) returns: number