Skip to content

JulesLabs/pubby-subby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

import PubSub from 'pubby-subby'

let pubsub = new PubSub()

At this point, we can start listening for events:

function doStuff() {
	console.log('I did stuff')
}

pubsub.register('doStuff', doStuff)

And trigger them, too:

pubsub.dispatch('doStoff') // => 'I did stuff!'

We can register multiple actions at a time:

function doMoreStuff() {
	console.log('I did more stuff!')
}

pubsub.register('doStuff', doMoreStuff)

pubsub.dispatch('doStuff') // => 'I did stuff!', 'I did more stuff'

When we no longer want to call an action when an event is dispatched:

pubsub.unregister('doStuff', doMoreStuff)

And we can also delete an entire topic:

pubsub.deleteTopic('doStuff')

Releases

No releases published

Packages

No packages published