https://github.com/johnsylvain/singleton-pubsub
A performant pub/sub interface wrapped in a singleton
https://github.com/johnsylvain/singleton-pubsub
high-performance pubsub singleton utility-library
Last synced: 2 days ago
JSON representation
A performant pub/sub interface wrapped in a singleton
- Host: GitHub
- URL: https://github.com/johnsylvain/singleton-pubsub
- Owner: johnsylvain
- License: mit
- Created: 2017-11-11T00:18:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-21T01:51:08.000Z (over 8 years ago)
- Last Synced: 2025-11-27T10:30:06.674Z (7 months ago)
- Topics: high-performance, pubsub, singleton, utility-library
- Language: JavaScript
- Homepage: https://npm.im/singleton-pubsub
- Size: 119 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Singleton Pubsub
[](https://travis-ci.org/johnsylvain/singleton-pubsub)
[](https://npmjs.org/package/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
```bash
# via yarn
yarn add singleton-pubsub
# via npm
npm install singleton-pubsub --save
```
#### or via [download](https://raw.githubusercontent.com/johnsylvain/singleton-pubsub/master/index.js)
```html
```
## Usage
```js
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](https://github.com/johnsylvain/singleton-pubsub/blob/master/LICENSE)