2.2.2 • Published 6 years ago

@lbzg/pubsub v2.2.2

Weekly downloads
1
License
ISC
Repository
bitbucket
Last release
6 years ago

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

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago