0.0.1 • Published 6 years ago

@kofijs/dispatch v0.0.1

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

@kofijs/dispatch

A delightful library to register and trigger custom named events.

npm npm npm pr

Installation

Use nmp to install this package:

$ npm install --save @kofijs/dispatch

Use it in your HTML files:

<!-- Develop version -->
<script type="text/javascript" src="./node_modules/@kofijs/dispatch/kofi-dispatch.js"></script>

<!-- Minified version -->
<script type="text/javascript" src="./node_modules/@kofijs/dispatch/kofi-dispatch.min.js"></script>

Use it in your ES6 modules:

import {dispatch} from "@kofijs/dispatch";

Example

//Initialize the new dispatcher
let dispatcher = kofi.dispatch();

//Register a listener for the event called 'foo'
dispatcher.addListener("foo", function (text) {
    console.log("New text: " + text);
});

//Emit the event called 'foo' with a string as a argument
dispatcher.emit("foo", "Hello world!");

API

var dispatcher = kofi.dispatch();

Generates a new dispatcher.

var dispatcher = kofi.dispatch();

dispatcher.addListener(name, listener)

Registers a new listener function to the event called name.

dispatcher.addListener("error", function (message) {
    console.log("New error generated: " + message);
});

dispatcher.removeListener(name, listener)

Removes the specific listener function from the event called name.

dispatcher.removeAllListeners(name)

Removes all listeners of the event called name.

dispatcher.emit(name, args...)

Trigger all listeners of the event called name. All the extra arguments passed to this function will be passed to all registered listeners.

dispatcher.emit("error", "Error importing file xxxx.json");

License

Under the MIT LICENSE.