0.1.0 • Published 6 years ago

js-event-dispatcher v0.1.0

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

js-event-dispatcher

Build Status Coverage Status

A simple js event dispatcher which implements the mediator pattern

Install

$ npm install --save eventdispatcher

Usage

    import EventDispatcher from 'js-event-dispatcher'

    let mycb = (evtName, param) => console.log(evtName, param)
    EventDispatcher.register('myEvent', mycb)

    EventDispatcher.emit('myEvent', 'lol') // logs 'myEvent', 'lol'

    EventDispatcher.unregister('myEvent', mycb)

For browser usage just add dist/EventDispatcher.js in your document, i.e.

<head>
    <script src="dist/EventDispatcher.js"></script>
</head>

Methods

register

Registers a function to an event

register(evtName, callback, bind?)
ParamTypeDescription
evtNameStringjust the event name
callbackFunctioncallback called when the evtName is emitted
bindObjectThe object to which will point this keyword inside the callback, if empty callback will binded to EventDispatcher object

The callback will be called with the following parameters:

callback(evtName, ...params)

Where ...params may be injected by who emits the event

unregister

Unregisters all functions from an event, or just the given one

unregister(evtName, callback?)
ParamTypeDescription
evtNameStringthe event name
callbackFunctionif given only that callback will be unregistered

emit

Emits an event

emit(evtvName, params?)
ParamTypeDescription
evtNameStringthe event name
paramsMixedother parameters that will be passed to the registered callbacks

Testing

Run tests with coverage report

$ npm tun test

Update html report

$ npm run coverage-html