maosdk
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Heimdall

Heimdall is a simple and light-weight package that implements the Observer Pattern

Installation

$ npm install heimdall-js

or

$ yarn add heimdall-js

Usage

Notify all

You can notify all registered observers in a subject:

import Heimdall from 'heimdall-js';

// First param is just the observer name (used to create the context)
const keyboardListener = Heimdall.subject('keyboard', (notify) => {
    function handleKeydown({ key }) {
        notify({ key });
    }

    document.addEventListener('keydown', handleKeydown);
});

keyboardListener.subscribe((data) => console.log(data));

Notify specific observers

But if you want to notify only one or more specific observers... Yes, you can do it!

In this case, you will need to add a name to your observers, like on the last line of code:

import Heimdall from 'heimdall-js';

const keyboardListener = Heimdall.subject('keyboard', (notify) => {
    function handleKeydown({ key }) {
        notify({ key }, 'observer1'); // Notify only one observer
        // or...
        // notify({ key }, ['observer1', 'observer2']); // Notify multiple observers
    }

    document.addEventListener('keydown', handleKeydown);
});

// Set the observer name as the first param
keyboardListener.subscribe('observer1', (data) => console.log(data));

Unsubscribe observers

If you want to unsubscribe a single observer you'll need to name him, but, you can run the ubsubscribeAll method to remove all observers (named or not).

import Heimdall from 'heimdall-js';

const keyboardListener = Heimdall.subject('keyboard', (notify) => {
    function handleKeydown({ key }) {
        notify({ key });
    }

    document.addEventListener('keydown', handleKeydown);
});

keyboardListener.subscribe((data) => console.log(data));
keyboardListener.subscribe('observer2', (data) => console.log(data));

keyboardListener.unsubscribe('observer2');
keyboardListener.unsubscribeAll();

That's all folks!

Simple like everything should be!

Readme

Keywords

none

Package Sidebar

Install

npm i maosdk

Weekly Downloads

0

Version

2.0.1

License

ISC

Unpacked Size

13.1 kB

Total Files

16

Last publish

Collaborators

  • mateusilva