Giter VIP home page Giter VIP logo

events's Introduction

@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()

events's People

Contributors

andrew-colman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.