Skip to content

Simple pubsub lightweight library using Window event listener and storage event

Notifications You must be signed in to change notification settings

griffithtp/windowpubsub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

windowpubsub

npm version npm MIT license

Simple and lightweight Pub/Sub library using Window event listener and storage event with zero dependencies.

This enable applications to subscribe to events across different windows or tabs. Some use cases are for shopping carts updates or theme switching.

Subscribing

subscribe(topic, callbackFunction);

import localpubsub from 'localpubsub';

function displayMessage(data) {
  console.log(data)
}
localpubsub.subscribe('my topic', displayMessage);

Publishing

publish(topic, message);

localpubsub.publish('my topic', 'Hello');

Unsubscribe

unsubscribe(topic);

const subscription = localpubsub.subscribe(topic, displayMessage);
subscription.unsubscribe();

localpubsub.unsubscribe(topic);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT