|
| 1 | +/** |
| 2 | + * Converts the passed object, or a new object, into an event emitter |
| 3 | + * @param {object} obj The object to convert |
| 4 | + * @returns {object} The passed object for chaining |
| 5 | + */ |
| 6 | +telegraph = function (obj) { |
| 7 | + 'use strict'; |
| 8 | + |
| 9 | + obj = obj || {}; |
| 10 | + |
| 11 | + // we store the list of handlers as a local variable inside the scope so |
| 12 | + // that we don't have to add random properties to the object we are |
| 13 | + // wrapping. (prefixing variables in the object with an underscore or two is |
| 14 | + // an ugly solution) |
| 15 | + var handlers = {}; |
| 16 | + |
| 17 | + /** |
| 18 | + * Add a listener |
| 19 | + * @param {string} eventName The name of the event |
| 20 | + * @param {function} handler The handler function for the event |
| 21 | + * @returns {object} This object for chaining |
| 22 | + */ |
| 23 | + obj.on = function (eventName, handler, front) { |
| 24 | + // either use the existing array or create a new one for this event |
| 25 | + (handlers[eventName] = handlers[eventName] || []) |
| 26 | + // add the handler to the array |
| 27 | + [front ? 'unshift' : 'push'](handler); |
| 28 | + |
| 29 | + return obj; |
| 30 | + }; |
| 31 | + |
| 32 | + /** |
| 33 | + * Add a listener that will only be called once |
| 34 | + * @param {string} eventName The name of the event |
| 35 | + * @param {function} handler The handler function for the event |
| 36 | + * @returns {object} This object for chaining |
| 37 | + */ |
| 38 | + obj.once = function (eventName, handler, front) { |
| 39 | + // create a wrapper listener, that will remove itself after it is called |
| 40 | + function wrappedHandler() { |
| 41 | + // remove ourself, and then call the real handler with the args |
| 42 | + // passed to this wrapper |
| 43 | + handler.apply(obj.off(eventName, wrappedHandler), arguments); |
| 44 | + } |
| 45 | + |
| 46 | + // in order to allow that these wrapped handlers can be removed by |
| 47 | + // removing the original function, we save a reference to the original |
| 48 | + // function |
| 49 | + wrappedHandler.h = handler; |
| 50 | + |
| 51 | + // call the regular add listener function with our new wrapper |
| 52 | + return obj.on(eventName, wrappedHandler, front); |
| 53 | + }; |
| 54 | + |
| 55 | + /** |
| 56 | + * Remove a listener. Remove all listeners for eventName if handler is |
| 57 | + * omitted. Remove all listeners for all event names if eventName is also |
| 58 | + * omitted. |
| 59 | + * @param {string} eventName The name of the event |
| 60 | + * @param {function} handler The handler function for the event |
| 61 | + * @returns {object} This object for chaining |
| 62 | + */ |
| 63 | + obj.off = function (eventName, handler) { |
| 64 | + // if no eventName, clear all event handlers for all events |
| 65 | + if (eventName === undefined) { |
| 66 | + handlers = {}; |
| 67 | + return obj; |
| 68 | + } // if |
| 69 | + |
| 70 | + // loop through all handlers for this eventName to see if the handler |
| 71 | + // passed in was any of them so we can remove it |
| 72 | + // if no handler, clear all handlers for the event instead |
| 73 | + var list = handler ? handlers[eventName] || [] : []; |
| 74 | + for (var i = 0; i < list.length; i++) { |
| 75 | + // either this item is the handler passed in, or this item is a |
| 76 | + // wrapper for the handler passed in. See the 'once' function |
| 77 | + if (list[i] === handler || list[i].h === handler) |
| 78 | + list.splice(i--, 1); |
| 79 | + } // for( i ) |
| 80 | + |
| 81 | + // cleanup if no events for the eventName |
| 82 | + if (!list.length) { |
| 83 | + // remove the array for this eventname (if it doesn't exist then |
| 84 | + // this isn't really hurting anything) |
| 85 | + delete handlers[eventName]; |
| 86 | + } // if |
| 87 | + |
| 88 | + return obj; |
| 89 | + }; |
| 90 | + |
| 91 | + /** |
| 92 | + * Dispatches the named event, calling all registered handler functions. If |
| 93 | + * any handler returns false, the event subsequent handlers are not called |
| 94 | + * and false is returned; Otherwise, all handlers are called and true is |
| 95 | + * returned. |
| 96 | + * @param {string} eventName The name of the event to dispatch |
| 97 | + * @returns {boolean} False if any handler returns false, true otherwise. |
| 98 | + */ |
| 99 | + obj.emit = function (eventName) { |
| 100 | + // loop through all handlers for this event name and call them all |
| 101 | + // arguments is "array-like", so call slice() from list instead |
| 102 | + // handlers can return false to cancel event |
| 103 | + var list = handlers[eventName] || []; |
| 104 | + var args = list.slice.call(arguments, 1); |
| 105 | + for (var i = 0; i < list.length; ++i) { |
| 106 | + if (list[i].apply(obj, args) === false) |
| 107 | + return false; |
| 108 | + } // for( i ) |
| 109 | + |
| 110 | + return true; |
| 111 | + }; |
| 112 | + |
| 113 | + return obj; |
| 114 | +} // telegraph( ) |
0 commit comments