Socket
Book a DemoInstallSign in
Socket

@dkx/event-dispatcher

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dkx/event-dispatcher

Event dispatcher for node.js

latest
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

DKX/EventDispatcher

Event dispatcher for node.js

Installation

Dependencies:

  • reflect-metadata

with npm:

$ npm install --save @dkx/event-dispatcher

or with yarn:

$ yarn add @dkx/event-dispatcher

Events

Events should be small value object classes implementing the Event interface.

import {Event} from '@dkx/event-dispatcher';

class UserUpdatedEvent implements Event
{
    constructor(
        public readonly user: User,
        public readonly changes: any,
    ) {}
}

Subscribers

Subscribers are classes which contains subscriptions to defined events. They should implement the Subscriber interface.

import {Subscriber, Subscribe} from '@dkx/event-dispatcher';
import {UserUpdatedEvent} from './user-updated-event';

class UserUpdatedEmailNotification implements Subscriber
{
    @Subscribe()
    public onUserUpdated(event: UserUpdatedEvent): void
    {
        console.log(event);
        // todo send email
    }
}

Event to which the subscriber should subscribe is taken from method argument type (event: UserUpdatedEvent).

Subscription method can be also async and return Promise.

Call event

import {EventDispatcher} from '@dkx/event-dispatcher';
import {UserUpdatedEvent} from './user-updated-event';
import {UserUpdatedEmailNotification} from './user-updated-email-notification';

// create and register first subscriber
const events = new EventDispatcher;
events.addSubscriber(new UserUpdatedEmailNotification);

// dispatch event
events.dispatch(new UserUpdatedEvent(user, {}));

FAQs

Package last updated on 04 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts