Skip to content

Instantly share code, notes, and snippets.

@SplittyDev
Created August 12, 2016 21:57
Show Gist options
  • Save SplittyDev/f3c156f00f52b4ae1eca6a12afa1fa2c to your computer and use it in GitHub Desktop.
Save SplittyDev/f3c156f00f52b4ae1eca6a12afa1fa2c to your computer and use it in GitHub Desktop.
eva.js - The lightweight es6 event library.
// eva.js - The lightweight es6 event library.
var eva = (() => {
// Prepare local globals
var eva = {};
var receivers = [];
var gid = 0;
/**
* Verify the type of a parameter.
* Throw if the type does not match the expected type.
* @param {object} item - The argument itself
* @param {string} name - The name of the argument
* @param {string} expected - The expected type
*/
var verifyType = (item, name, expected) => {
if (typeof (item) !== expected) {
throw "Argument '" + name + "' has to be of type '" + expected + "'.";
}
}
/**
* Subscribe to an event.
* @param {string} name - The name of the event
* @param {function} action - The event action
* @returns {number} - The subscription identifier
*/
var subscribe = (name, action) => {
verifyType(name, 'name', 'string');
verifyType(action, 'action', 'function');
receivers.push({name: name, action: action, id: gid});
return gid++;
}
/**
* Unsubscribe from an event.
* @param {number} id - The subscription identifier
* @returns {boolean} - Whether the subscription has been removed
*/
var unsubscribe = id => {
verifyType(id, 'id', 'number');
var result = false;
receivers.every((item, index, arr) => {
if (item['id'] === id) {
arr.splice(index, 1);
result = true;
}
return !result;
});
return result;
}
/**
* Dispatch an event.
* @param {string} name - The name of the event
* @param {object} e - The event data
*/
var dispatch = (name, e) => {
verifyType(name, 'name', 'string');
receivers.forEach(item => item['action']({event: name, data: e}));
};
/**
* List all receivers.
* @returns {array} - The receivers
*/
var list = () => receivers;
/**
* Clear all receivers.
*/
var clear = () => {
receivers.length = 0;
};
// Make functions accessible
eva.subscribe = subscribe;
eva.unsubscribe = unsubscribe;
eva.dispatch = dispatch;
eva.clear = clear;
eva.list = list;
// Return eva object
return eva;
}());
module.exports = eva;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment