@arturdoruch/event-dispatcher

1.0.1 • Public • Published

Event dispatcher

Dispatches custom events to registered listeners.

Install

yarn add @arturdoruch/event-dispatcher

Usage

import eventDispatcher from '@arturdoruch/event-dispatcher';

let uploadListener = {
    onComplete(argument1, argument2, argument3) {
        console.log('First listener: ', argument1, argument2, argument3);
        console.log('Context: ', this);
        console.log();
    }
}

function uploadCompleteSecondListener(argument1, argument2) {
    console.log('Second listener: ', argument1, argument2);
    console.log('Context: ', this);
    console.log();
}

// Register listeners. The third argument is a listener context.
eventDispatcher.addListener('upload.complete', uploadListener.onComplete, uploadListener);
eventDispatcher.addListener('upload.complete', uploadCompleteSecondListener);

// Dispatch "upload.complete" event to two registered listeners.
eventDispatcher.dispatch('upload.complete', ['firstArgument', 'secondArgument']);

// Remove listener.
eventDispatcher.removeListener('upload.complete', uploadCompleteSecondListener);

// Dispatch "upload.complete" event to one registered listeners.
eventDispatcher.dispatch('upload.complete', [1, 2, 3]);

Package Sidebar

Install

npm i @arturdoruch/event-dispatcher

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

4.08 kB

Total Files

3

Last publish

Collaborators

  • arturdoruch1