@lbzg/pubsub v2.2.2
Javascript publish subscribe.
About
Javascript Publisher class for creating pub-sub relations.
Installation & Usage
npm i @lbzg/pubsubimport { Publisher } from '@lbzg/pubsub'
const pub = new Publisher()
const sub = (function() {
this.id = 'q'
this.data = null
this.setData = x => this.data = x
this.notify = payload => this.data = payload # notification function
return this
})();
pub.subscribe(sub)
# subscribe to 'default' topic with 'notify' method
# sub.data === null
pub.publish('asdf')
# triggers registered notification methods on 'default' topic subscribers
# sub.data === 'asdf'
pub.trigger('setData', 'qwe')
# trigger subscribers method (with ...args) directly
# sub.data === 'qwe'# subscribing custom topics and methods
pub.subscribe(sub, 'weather-change', 'updateWeather')
# subscribing multiple topics
pub.subscribe(sub, ['topic1', 'topic2', 'topic3'])
# subscribing nested methods
pub.subscribe(sub, 'topic', 'prop/method')Prototype
Publisher
Publisher class. Implements IPublisher.
constructor(silentErrors = false)
silentErrors: boolean - ignores errors caused by unexisting notification method
subscribe(
subscriber,topics?= 'default',method?= 'notify', identifier? = 'id')
unsubscribe(subscriber,topics?,methods?, identifier? = 'id')
publish(payload?,topic?= 'default', id?)
trigger(triggerMethod, ...args) ~ triggerMethod can be a method name or topic, method, id? tuple/triplegetSub(id): subscriber | null
getSubs(selector?): array
subscriber: ISubscribertopic: stringtopics: string | string[]method: stringmethods: string | string[]identifier: stringpayload: anytriggerMethod: string | topic, method, id?
id: string | number ~ subscriber id
selector: id | id[] | filter func ~ filter function iterates subscribers and selects by returning truthy
It is possible to subscribe nested methods like this 'very/nested/method'.
Trigger method is triggered on everyone unless topic, method, id? is provided as 1st argument.
Subscriber
Object subscribing for notifications. Implements ISubscriber or similar.
id: string | number
notify(payload, topic)
Subscriber should implement:
X unique identifier
X any notification method(s)
References
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago