Notifly

A very minimal dependency-free observer pattern implementation.


Keywords
observer pattern, observer, design pattern, pubsub, pub sub, pub, sub, notifications, events
License
MIT
Install
npm install Notifly@1.1.0

Documentation

Notifly

A very minimal dependency-free observer pattern implementation.


Installation

The library may be installed from NPM using the following command:

npm install --save Notifly

Or, if you would rather -- it may be included directly inline within your HTML document.

<script src="./dist/notifly.min.js></script>

API Methods

subscribe(topic, handler)

Subscribe a handler to a specific topic.

Parameters

  • topic string topic/channel to subscribe for.
  • handler function callback responsible for handling data sent to the topic/channel.

Examples

Creating a new subscription.

const handler = Notifly.subscribe('user.signup', (data) => {
     console.log(data.name); // John Doe
})

Unsubscribe a subscription.

handler.unsubscribe();

Returns function unsubscribe - Provides a function to unsubscribe the subscribed handler.


notify(topic, data)

Notify a specific topic/channel with data.

Parameters

  • topic string topic/channel to notify.
  • data object data to provide to the callback function of the subscriber.

Examples

Notifying a specific topic/channel

try {
 Notifly.notify('user.signup', {"name":"John Doe"});
} catch (error) {
 console.log(error.name); // TopicDoesNotExistException
 console.log(error.message); // user.signup topic does not exist.
}
  • Throws TopicDoesNotExistException