1.0.0 • Published 6 years ago

singleton-pubsub v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Singleton Pubsub

Travis npm dependancies

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

1.0.0

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

7 years ago

0.0.1

7 years ago