ActorPubSub is a dependency free publish/subscribe library for JavaScript based on the Actor Model. It takes a lot of inspiration from PubSubJS and uses the ideas from Actors Make Better Observers to implement the required actors. It can be used as a normal pub/sub library as all the actor logic is hidden away.
A major benefit of this implementation is that observer list updates do not block the notification process.
- Dependency free.
- Synchronization decoupling.
- Attaching and detaching subscribers is also asynchronous.
- Since observer list updates are asynchronous, notify operations are possible while updates are in progress.
- Data passed to subscribers is a copy of the original data, thus, changes to it by the subscribers does not affect other subscribers and the publisher.
var subscriberCb = function (msg, data) {
console.log(msg, data);
};
var token = PubSub.subscribe('Topic', subscriberCb);
PubSub.publish('Topic', 'ahoy!');
// create a function to receive the topic
var mySubscriber = function( msg, data ){
console.log(msg, data);
};
// add the function to the list of subscribers to a particular topic
// we're keeping the returned token, in order to be able to unsubscribe
// from the topic later on
var token = PubSub.subscribe('MY TOPIC', mySubscriber);
// unsubscribe this subscriber from this topic
PubSub.unsubscribe('MY TOPIC', token);