pubsub-simplified-js
v1.0.2
Published
Dependency free pubsub model based library
Downloads
6
Maintainers
Readme
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:
- true -> if such function is associated with any event and detached successfully
- false -> if no such function is associated with any event
Get all subscribers for a particular event
pubsubClass.getSubscribers(eventName1) returns:
- [] -> if no sch event is there (any event can't exist without <= 1 callback associated with it)
- [cb1, cb2, ......] -> all callbacks associated
Get all subscribers count for a particular event
pubsubClass.getSubscribersCount(eventName1) returns: number