npm.io
1.0.5 • Published 6 years ago

@antyper/simple-event-bus

Licence
ISC
Version
1.0.5
Deps
3
Size
47 kB
Vulns
0
Weekly
0

Simple Event Bus

This package help you emit events and handle it in one place. Whole project is written in TypeScript and RxJs.

Library is compatible with Inversify. You can initialize your EventBus in IOC container.

Example with Inversify

import {EventBus} from "./EventBus";
import {Container} from "inversify";

export const getContainer: () => Container = () => {
    const container: Container = new Container();

    container.bind<TestEventListener>('TestEventListener')
        .to(TestEventListener)
        .inRequestScope();
    
    container.bind<EventBus>('EventBus')
        .toDynamicValue((context) => {
            const eventBus: EventBus = new EventBus();
    
            eventBus.addEvent(TestEvent);
            eventBus.addEventListener(
                TestEvent,
                context.container.get('TestEventListener'),
            );
    
            return eventBus;
        })
        .inRequestScope();
}

const container: Container = getContainer();

const eventBus: EventBus = container.get<EventBus>('EventBus');
// Events are here handled 
eventBus.handle();

const testEvent: TestEvent = new TestEvent();
// Here is your event emitted
eventBus.emitEvent(testEvent);

Simple First Example

Keywords