Skip to content

xtitusx/event-bus

Repository files navigation

event-bus

NPM version CI Coverage Security badge Known vulnerabilities Downloads Minified size

event-bus handles events, on a pub-sub design pattern basis.

🚀 Node.js ready for launch.

Table of Contents

  1. Installation
  2. Basic Usage
  3. EventBus
  4. Subscription
  5. TypeDoc
  6. Changelog
  7. Maintainer
  8. License

Installation

npm install @xtitusx/event-bus

Basic Usage

  • Inherit a subclass from Event abstract class:
export class HelloWorldEvent extends Event {}
  • Declare a custom type message:
export type HelloWorldMessage = { name?: string };
  • Inherit a subclass from Subscriber abstract class:
export class HelloWorldEventSubscriber extends Subscriber {
    constructor() {
        super();
    }

    /**
     * @override
     */
    public implCallback(): Function {
        return async (message: HelloWorldMessage): Promise<void> => {
            console.log(`Hello ${message?.name ?? 'World'}!`);
        };
    }
}
  • Then you can play around with an EventBus instance:
const eventBus = new EventBus();
eventBus.subscribe(HelloWorldEvent, new HelloWorldEventSubscriber());
eventBus.publish(HelloWorldEvent);

// => 'Hello World!'

Optional Singleton

Although the singleton is considered to be an anti pattern as it's introducing global states, it can be useful in some use cases, like a lack of dependency injection:

EventBus.getInstance().subscribe(HelloWorldEvent, new HelloWorldEventSubscriber());
EventBus.getInstance().publish(HelloWorldEvent);

// => 'Hello World!'

EventBus

subscribe()

Adds a subscriber for the specified event. No checks are made to see if the subscriber has already been added.

Multiple calls passing the same combination of event and subscriber will result in the subscriber being added multiple times:

const eventBus = new EventBus();
eventBus.subscribe(HelloWorldEvent, new HelloWorldEventSubscriber());
eventBus.publish(HelloWorldEvent);

// => 'Hello World!'

eventBus.publish<HelloWorldMessage>(HelloWorldEvent, {
    name: 'Benjamin',
});

// => 'Hello Benjamin!'  

once()

Adds a one time subscriber to the event. This subscriber is invoked only the next time the event is fired, after which it is removed:

const eventBus = new EventBus();
eventBus.once(HelloWorldEvent, new HelloWorldEventSubscriber());
eventBus.publish<HelloWorldMessage>(HelloWorldEvent, {
    name: 'Chorizo',
});

// => 'Hello Chorizo!'    

eventBus.publish<HelloWorldMessage>(HelloWorldEvent, {
    name: 'Chorizo',
});

// => Nothing

clear()

  • Removes all subscribers:
const eventBus = new EventBus();
eventBus.subscribe(HelloWorldEvent, new HelloWorldEventSubscriber());
eventBus.publish<HelloWorldMessage>(HelloWorldEvent, {
    name: 'Benjamin',
});

// => 'Hello Benjamin!' 

eventBus.clear();

eventBus.publish<HelloWorldMessage>(HelloWorldEvent, {
    name: 'Benjamin',
});

// => Nothing
  • Or removes only subscribers for the specified event:
const eventBus = new EventBus();
eventBus.subscribe(HelloWorldEvent, new HelloWorldEventSubscriber());
eventBus.publish<HelloWorldMessage>(HelloWorldEvent, {
    name: 'Benjamin',
});

// => 'Hello Benjamin!' 

eventBus.clear(HelloWorldEvent);

eventBus.publish<HelloWorldMessage>(HelloWorldEvent, {
    name: 'Benjamin',
});

// => Nothing

Subscription

unsubscribe()

Removes the subscriber for the specified event:

const eventBus = new EventBus();
const subscription = eventBus.subscribe(HelloWorldEvent, new HelloWorldEventSubscriber());
eventBus.publish(HelloWorldEvent);

// => 'Hello World!' 

subscription.unsubscribe();

eventBus.publish<HelloWorldMessage>(HelloWorldEvent, {
    name: 'Chorizo',
});

// => Nothing

TypeDoc

GitHub HTML Preview

Changelog

See information about breaking changes and release notes here.

Maintainer

  • xtitusx - Benjamin Tussac (author)

License

MIT License

Copyright (c) 2021 Benjamin Tussac

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.