This package has been deprecated

Author message:

EOL

@lbzg/pubsub
TypeScript icon, indicating that this package has built-in type declarations

2.2.2 • Public • Published

Javascript publish subscribe.

About

Javascript Publisher class for creating pub-sub relations.

Installation & Usage

npm i @lbzg/pubsub
import { 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/triple

getSub(id): subscriber | null
getSubs(selector?): array

subscriber: ISubscriber
topic: string
topics: string | string[]
method: string
methods: string | string[]
identifier: string
payload: any
triggerMethod: 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

lbzg/arrays
lbzg/collections

Package Sidebar

Install

npm i @lbzg/pubsub

Weekly Downloads

1

Version

2.2.2

License

ISC

Unpacked Size

85.4 kB

Total Files

19

Last publish

Collaborators

  • lbzg