Pubby Subby
Pubby subby is an implementation of the JavaScript pub/sub
model.
But what is Pub/Sub?
Pub/Sub (short for publish / subscribe
) is a model which is really useful for modular JavaScript, which implies the division of the code into smaller units, or modules, which makes everything more manageable.
It consists in the creation of a global object which keeps track of a list of "events" (or topics, as pubby-subby calls them) and "handlers" (or actions, as pubby-subby calls them), and lets other parts of the app "subscribe" and "unsubscribe" from them. This particoular implementation has a really simple API, whith few, sufficient and easy-to-use features.
Getting started
First of all, you'll want to create an instance of the pubby-subby
class:
let pubsub =
At this point, we can start listening for events:
{ console} pubsub
And trigger them, too:
pubsub // => 'I did stuff!'
We can register multiple actions at a time:
{ console} pubsub pubsub // => 'I did stuff!', 'I did more stuff'
When we no longer want to call an action when an event is dispatched:
pubsub
And we can also delete an entire topic:
pubsub