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

1.0.2 • Public • Published

Perceiver

Ridiculously simple implementation of the observer pattern

Installing perceiver:

npm install --save perceiver

Importing perceiver to your project:

import { Perceiver } from 'perceiver';
// or const { Perceiver } = require('perceiver');

const MyObserver = Perceiver();
MyObserver.subscribe(() => console.log('Listener one'));
MyObserver.subscribe(() => console.log('Listener two'));
MyObserver.subscribe(() => console.log('Listener three'));

MyObserver.notify();

Every listener is invoked with the same arguments as passed to notify() Example with passing data to notify() function:

import { Perceiver } from 'perceiver';

const MyObserver = Perceiver();
MyObserver.subscribe((eventId, eventName, eventPayload) => {
  console.log('Listener one', eventId, eventName, eventPayload)
});

MyObserver.subscribe((eventId, eventName, eventPayload) => {
  console.log('Listener two', eventId, eventName, eventPayload)
});

MyObserver.subscribe((eventId, eventName, eventPayload) => {
  console.log('Listener three', eventId, eventName, eventPayload)
});

MyObserver.notify(23, 'event name', { foo: 'bar' });

Typescript example:

import { Perceiver } from 'perceiver';

type CustomListenerFn = (id: number, name: string) => void;

const MyObserver = Perceiver<CustomListenerFn>();
MyObserver.subscribe((id: number, name: string) => console.log('listener: ', id, name));

Dependents (0)

Package Sidebar

Install

npm i perceiver

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

21.6 kB

Total Files

16

Last publish

Collaborators

  • mtronix