singleton-pubsub
v1.0.0
Published
A performant pub/sub interface wrapped in a singleton
Downloads
2
Readme
Singleton Pubsub
A performant Pub/Sub interface wrapped in a singleton
Features
- Super tiny (~210b g-zipped)
- No dependencies
- Small API with method chaining
- Singleton impletation for usage in large applications
- Create multiple functions for a single event
Installation
# via yarn
yarn add singleton-pubsub
# via npm
npm install singleton-pubsub --save
or via download
<script src="path/to/singleton-pubsub.js"></script>
Usage
import SingletonPubsub from 'singleton-pubsub'
// Instantiate a default instance
const pubsub = new SingletonPubsub();
// Instantiate a named instance
const altPubsub = new SingletonPubsub('alternate');
// Subscribe to events via handlers
pubsub
.on('load', ({ message }) => {
console.log(message) // => 'loaded!'
})
.on('event', (data) => {
console.log(data)
})
// Emit (publish) events
pubsub
.emit('load', {
message: 'loaded!'
})
// Unsubscribe from an event
const onEvent = (data) => console.log(data)
pubsub
.on('event', onEvent)
.off('event', onEvent)
// Creating a new instance will give access to the same events
const pubsub2 = new SingletonPubsub()
pubsub2
.emit('load', {
message: 'pubsub2 works!'
})
// Create a new clean instance in the same application
const cleanPubsub = new SingletonPubsub('instance', {
reinstantiate: true
})
Contributing
You can request a new feature by submitting an issue. If you would like to implement a new feature feel free to issue a Pull Request.
License
singleton-pubsub is protected under the MIT License