diff options
Diffstat (limited to 'node_modules/nanobus/index.js')
| -rw-r--r-- | node_modules/nanobus/index.js | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/node_modules/nanobus/index.js b/node_modules/nanobus/index.js new file mode 100644 index 00000000..ccd01295 --- /dev/null +++ b/node_modules/nanobus/index.js @@ -0,0 +1,162 @@ +var splice = require('remove-array-items') +var nanotiming = require('nanotiming') +var assert = require('assert') + +module.exports = Nanobus + +function Nanobus (name) { + if (!(this instanceof Nanobus)) return new Nanobus(name) + + this._name = name || 'nanobus' + this._starListeners = [] + this._listeners = {} +} + +Nanobus.prototype.emit = function (eventName) { + assert.equal(typeof eventName, 'string', 'nanobus.emit: eventName should be type string') + + var data = [] + for (var i = 1, len = arguments.length; i < len; i++) { + data.push(arguments[i]) + } + + var emitTiming = nanotiming(this._name + "('" + eventName + "')") + var listeners = this._listeners[eventName] + if (listeners && listeners.length > 0) { + this._emit(this._listeners[eventName], data) + } + + if (this._starListeners.length > 0) { + this._emit(this._starListeners, eventName, data, emitTiming.uuid) + } + emitTiming() + + return this +} + +Nanobus.prototype.on = Nanobus.prototype.addListener = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.on: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.on: listener should be type function') + + if (eventName === '*') { + this._starListeners.push(listener) + } else { + if (!this._listeners[eventName]) this._listeners[eventName] = [] + this._listeners[eventName].push(listener) + } + return this +} + +Nanobus.prototype.prependListener = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.prependListener: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.prependListener: listener should be type function') + + if (eventName === '*') { + this._starListeners.unshift(listener) + } else { + if (!this._listeners[eventName]) this._listeners[eventName] = [] + this._listeners[eventName].unshift(listener) + } + return this +} + +Nanobus.prototype.once = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.once: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.once: listener should be type function') + + var self = this + this.on(eventName, once) + function once () { + listener.apply(self, arguments) + self.removeListener(eventName, once) + } + return this +} + +Nanobus.prototype.prependOnceListener = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.prependOnceListener: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.prependOnceListener: listener should be type function') + + var self = this + this.prependListener(eventName, once) + function once () { + listener.apply(self, arguments) + self.removeListener(eventName, once) + } + return this +} + +Nanobus.prototype.removeListener = function (eventName, listener) { + assert.equal(typeof eventName, 'string', 'nanobus.removeListener: eventName should be type string') + assert.equal(typeof listener, 'function', 'nanobus.removeListener: listener should be type function') + + if (eventName === '*') { + this._starListeners = this._starListeners.slice() + return remove(this._starListeners, listener) + } else { + if (typeof this._listeners[eventName] !== 'undefined') { + this._listeners[eventName] = this._listeners[eventName].slice() + } + + return remove(this._listeners[eventName], listener) + } + + function remove (arr, listener) { + if (!arr) return + var index = arr.indexOf(listener) + if (index !== -1) { + splice(arr, index, 1) + return true + } + } +} + +Nanobus.prototype.removeAllListeners = function (eventName) { + if (eventName) { + if (eventName === '*') { + this._starListeners = [] + } else { + this._listeners[eventName] = [] + } + } else { + this._starListeners = [] + this._listeners = {} + } + return this +} + +Nanobus.prototype.listeners = function (eventName) { + var listeners = eventName !== '*' + ? this._listeners[eventName] + : this._starListeners + + var ret = [] + if (listeners) { + var ilength = listeners.length + for (var i = 0; i < ilength; i++) ret.push(listeners[i]) + } + return ret +} + +Nanobus.prototype._emit = function (arr, eventName, data, uuid) { + if (typeof arr === 'undefined') return + if (arr.length === 0) return + if (data === undefined) { + data = eventName + eventName = null + } + + if (eventName) { + if (uuid !== undefined) { + data = [eventName].concat(data, uuid) + } else { + data = [eventName].concat(data) + } + } + + var length = arr.length + for (var i = 0; i < length; i++) { + var listener = arr[i] + listener.apply(listener, data) + } +} |
