Package Exports
- pub-sub-es
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pub-sub-es) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Simple ES6-based pub-sub service
A simple 0.6KB pub-sub-based event library that lets you send custom message within and cross the window! It's written in ES6 and makes use of the awesome Broadcast Channel API.
Demo: pub-sub.lekschas.de
Install
npm -i -D pub-sub-esGet Started
import createPubSub from 'pub-sub-es';
// Creates a new pub-sub event listener stack.
const pubSub = createPubSub();
// Subscribe to an event
const myEmojiEventHandler = msg => console.log(`🎉 ${msg} 😍`);
pubSub.on('my-emoji-event', myEmojiEventHandler);
// Publish an event
pubSub.publish('my-emoji-event', 'Yuuhuu'); // >> 🎉 Yuuhuu 😍
// Unsubscribe
pubSub.unsubscribe('my-emoji-event', myEmojiEventHandler);API
pub-sub-es exports the factory function (createPubSub) for creating a new pub-sub service by default and a global pub-sub service (globalPubSub). The API is the same for both.
createPubSub(stack = { __times__: {} })
stackis an object holding for holding the event listeners and defaults to a new stack when being omitted.
Returns: a new pub-sub service
pubSub.publish(event, news)
eventis the name of the event to be published.newsis an arbitrary value that is being broadcasted.
pubSub.subscribe(event, handler, times)
eventis the name of the event to be published.handleris the handler function that is being called together with the broadcasted value.timesis the number of times the handler is invoked before it's automatically unsubscribed.
Returns: an object of form { event, handler }. This object can be used to automatically unsubscribe, e.g., pubSub.unsubscribe(pubSub.subscribe('my-event', myHandler)).
pubSub.unsubscribe(event, news)
eventis the name of the event to be published. Optionally,unsubscribeaccepts an object of form{ event, handler}coming fromsubscribe.newsis an arbitrary value that is being broadcasted.
Global pub-sub
The global pub-sub instance is created when pub-sub-es.js is loaded and provides a way for
different JS objects within the same tab to communitate with each other.
Development
Some handy commands:
npm build: builds the UMD librarynpm run watch: continuously builds the UMD librarynpm run lint: checks the code stylenpm test: runs the test suite