Eventable

Class for generic event handling.

EventHandler({
    events: {
        eventName: function () {},
        otherName: function () {}
    },
    singles: {
        anotherName: function () {}
    }
})
Params
[opts.events] Object.<string, function|Array.<function>> Event callbacks to fire each time the event is triggered.
[opts.singles] Object.<string, function|Array.<function>> Event callbacks to fire only once the next time the event is triggered.
Returns
EventHandler
module.exports = function (opts) {
    var events = {},
        singles = {},
        name,

triggerCache

Cache of events that have already been triggered once.

Type
Object.<string, *>
        triggerCache = {};

Load in the initial callbacks.

    opts = opts || {};
    for (name in opts.events) {
        events[name] = opts.events[name];
    }
    for (name in opts.singles) {
        singles[name] = opts.singles[name];
    }

    return {

handler.on(name, callback, [lazy])

Bind a callback to an event. Callback will run each time the event is triggered.

Params
name string Event name.
cb function(?) Event callback.
[lazy] boolean True if this callback should not fire immediately if the event has already been triggered.
        on: function (name, cb, lazy) {
            if (!lazy && name in triggerCache) {
                cb(triggerCache[name]);
            }
            events[name] = events[name] || [];
            events[name].push(cb);
        },

handler.one(name, callback, [lazy])

Bind a callback to an event. Callback will run only once the next time the event is triggered.

Params
name string Event name.
cb function(?) Event callback.
[lazy] boolean True if this callback should not fire immediately if the event has already been triggered.
        one: function (name, cb, lazy) {
            if (!lazy && name in triggerCache) {
                cb(triggerCache[name]);
            } else {
                singles[name] = singles[name] || [];
                singles[name].push(cb);
            }
        },

handler.off([name])

Clear all callbacks from an event.

Params
[name] string Event name. Empty to clear all callbacks from the system.
        off: function (name) {
            if (name) {
                events[name] = [];
                singles[name] = [];
            } else {
                events = {};
                singles = {};
                triggerCache = {};
            }
        },

handler.trigger(name, [data])

Immediately trigger an event.

Params
name string Event name.
[data] Any data you wish to provide to all event callbacks.
        trigger: function (name, data) {
            if (name in events) {
                events[name].forEach(function (cb) {
                    cb(data);
                });
            }
            if (name in singles) {
                singles[name].forEach(function (cb) {
                    cb(data);
                });
                singles[name] = [];
            }

Cache that this event has fired.

            triggerCache[name] = data;
        }
    };
};