@plancky/notifier
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

Pure notifier implementation

A Notifier is an object that has a notify property. It can have subscribers that listen for that notify call and react to it. It's like an event emitter that only emits one type of event.

It can be either used directly:

  import Notifier from '@plancky/notifier';

  const notifier = new Notifier();

  const subscriber = () => {
    console.log('Something happened!');
  }

  notifier.subscribe(subscriber);

  notifier.notify(); // Logs "Something happened"

Or extended by another class:

  import Notifier from '@plancky/notifier';

  class DataNotifier extends Notifier {
    data = "hello"

    setData = (newData) => {
      this.data = newData;
      this.notify();
    }
  }

  const dataNotifier = new DataNotifier();
  dataNotifier.subscribe(() => {
    console.log(`The data changed! It is now ${dataNotifier.data}`);
  });

  dataNotifier.setData('bye'); // Logs "The data changed! It is now bye"

Readme

Keywords

none

Package Sidebar

Install

npm i @plancky/notifier

Weekly Downloads

1

Version

0.3.0

License

MIT

Unpacked Size

9.08 kB

Total Files

14

Last publish

Collaborators

  • erodactyl