Skip to content
/ events Public

events system with observer pattern implementation: PubSub and Topic Based alternative

License

Notifications You must be signed in to change notification settings

reaxi/events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f73266f · Oct 5, 2022

History

4 Commits
Oct 1, 2022
Oct 1, 2022
Oct 5, 2022
Oct 5, 2022
Oct 1, 2022
Oct 1, 2022
Oct 1, 2022
Oct 1, 2022
Oct 5, 2022
Oct 1, 2022
Oct 5, 2022
Oct 1, 2022
Oct 1, 2022
Oct 5, 2022
Oct 1, 2022
Oct 1, 2022

Repository files navigation

@reaxi/events

  • events (system)

observer pattern implementation : PubSub and Topic Based alternative

- Pub (Publisher) & Sub (Subscriber)

import { PubSub } from '@reaxi/events';

const s = new PubSub();

s.subscribe(Function);

s.publish(payload);

s.unsubscribe(Function);

//example:

const myFn = (a: string) => console.log(a);

s.subscribe(myFn);

s.publish('Hello There');

- Topic Based (EventSystem)

import { EventSystem } from '@reaxi/events';

const ev = new EventSystem();

ev.register('topic', Function);

ev.emit('topic', PayloadData);

ev.unregister('topic', Function);

Example:

const ev = new EventSystem<string, any>(); // string payload, and any topic keys

const fn1 = a => console.log(a);
const fn2 = b => console.log(`data: ${b}`);
const fn3 = c => console.log(c.toUpperCase());

ev.register('topic 1', fn1);
ev.register('topic 2', fn2);
ev.register('topic 3', fn3);

ev.emit('topic 1', 'hello');
ev.emit('topic 1', 'hello');
ev.emit('topic 1', 'hello');

Typescript example:

const ev = new EventSystem<
    string, // payload type
    {
        ['preAction'];
        ['showMessage'];
        ['postAction'];
    } // topics types
>();

function setup() {
    ev.register('preAction', console.log('starting...'));
    ev.register('showMessage', message => console.log(message)); // will show "hello there"
    ev.register('postAction', () => console.log('done'));
}

function main() {
    ev.emit('preAction');
    //
    // program functions...
    //
    ev.emit('showMessage', 'hello there');
    ev.emit('postAction');
}

setup();
main();

Custom Class example:

type HooksPayload = string;

type Topics = {
    ['bootstrap'];
    ['onRequest'];
    ['onCleanup'];
};

class Hooks extends EventSystem<HooksPayload, Topics> {
    //... constructor, methods, properties
}

const hooks = new Hooks();

// EventSystem methods:
// hooks.register()
// hooks.emit()