event-bus handles events, on a pub-sub design pattern basis.
🚀 Node.js ready for launch.
npm install @xtitusx/event-bus
- 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!'
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!'
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!'
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
- 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
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
See information about breaking changes and release notes here.
- xtitusx - Benjamin Tussac (author)
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.