pax_global_header00006660000000000000000000000064130622550660014517gustar00rootroot0000000000000052 comment=b951397b8f0d55fc7ae8aea7fa7699e48132a53d event-emitter-0.3.5/000077500000000000000000000000001306225506600143145ustar00rootroot00000000000000event-emitter-0.3.5/.gitignore000066400000000000000000000000441306225506600163020ustar00rootroot00000000000000.DS_Store /.lintcache /node_modules event-emitter-0.3.5/.lint000066400000000000000000000001341306225506600152610ustar00rootroot00000000000000@root module es5 indent 2 maxlen 80 tabs ass plusplus nomen ./benchmark predef+ console event-emitter-0.3.5/.testignore000066400000000000000000000000131306225506600164720ustar00rootroot00000000000000/benchmark event-emitter-0.3.5/.travis.yml000066400000000000000000000004761306225506600164340ustar00rootroot00000000000000sudo: false # http://docs.travis-ci.com/user/workers/container-based-infrastructure/ language: node_js node_js: - 0.12 - 4 - 6 - 7 before_install: - mkdir node_modules; ln -s ../ node_modules/event-emitter notifications: email: - medikoo+event-emitter@medikoo.com script: "npm test && npm run lint" event-emitter-0.3.5/CHANGES000066400000000000000000000040421306225506600153070ustar00rootroot00000000000000v0.3.5 -- 2017.03.15 * Improve documentation * Update dependencies v0.3.4 -- 2015.10.02 * Add `emitError` extension v0.3.3 -- 2015.01.30 * Fix reference to module in benchmarks v0.3.2 -- 2015.01.20 * Improve documentation * Configure lint scripts * Fix spelling of LICENSE v0.3.1 -- 2014.04.25 * Fix redefinition of emit method in `pipe` * Allow custom emit method name in `pipe` v0.3.0 -- 2014.04.24 * Move out from lib folder * Do not expose all utilities on main module * Support objects which do not inherit from Object.prototype * Improve arguments validation * Improve internals * Remove Makefile * Improve documentation v0.2.2 -- 2013.06.05 * `unify` functionality v0.2.1 -- 2012.09.21 * hasListeners module * Simplified internal id (improves performance a little), now it starts with underscore (hint it's private). Abstracted it to external module to have it one place * Documentation cleanup v0.2.0 -- 2012.09.19 * Trashed poor implementation of v0.1 and came up with something solid Changes: * Improved performance * Fixed bugs event-emitter is now cross-prototype safe and not affected by unexpected methods attached to Object.prototype * Removed support for optional "emitter" argument in `emit` method, it was cumbersome to use, and should be solved just with event objects v0.1.5 -- 2012.08.06 * (maintanance) Do not use descriptors for internal objects, it exposes V8 bugs (only Node v0.6 branch) v0.1.4 -- 2012.06.13 * Fix detachment of listeners added with 'once' v0.1.3 -- 2012.05.28 * Updated es5-ext to latest version (v0.8) * Cleared package.json so it's in npm friendly format v0.1.2 -- 2012.01.22 * Support for emitter argument in emit function, this allows some listeners not to be notified about event * allOff - removes all listeners from object * All methods returns self object * Internal fixes * Travis CI integration v0.1.1 -- 2011.08.08 * Added TAD test suite to devDependencies, configured test commands. Tests can be run with 'make test' or 'npm test' v0.1.0 -- 2011.08.08 Initial version event-emitter-0.3.5/LICENSE000066400000000000000000000020701306225506600153200ustar00rootroot00000000000000Copyright (C) 2012-2015 Mariusz Nowak (www.medikoo.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. event-emitter-0.3.5/README.md000066400000000000000000000064071306225506600156020ustar00rootroot00000000000000# event-emitter ## Environment agnostic event emitter ### Installation $ npm install event-emitter To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/) ### Usage ```javascript var ee = require('event-emitter'); var MyClass = function () { /* .. */ }; ee(MyClass.prototype); // All instances of MyClass will expose event-emitter interface var emitter = new MyClass(), listener; emitter.on('test', listener = function (args) { // … react to 'test' event }); emitter.once('test', function (args) { // … react to first 'test' event (invoked only once!) }); emitter.emit('test', arg1, arg2/*…args*/); // Two above listeners invoked emitter.emit('test', arg1, arg2/*…args*/); // Only first listener invoked emitter.off('test', listener); // Removed first listener emitter.emit('test', arg1, arg2/*…args*/); // No listeners invoked ``` ### Additional utilities #### allOff(obj) _(event-emitter/all-off)_ Removes all listeners from given event emitter object #### hasListeners(obj[, name]) _(event-emitter/has-listeners)_ Whether object has some listeners attached to the object. When `name` is provided, it checks listeners for specific event name ```javascript var emitter = ee(); var hasListeners = require('event-emitter/has-listeners'); var listener = function () {}; hasListeners(emitter); // false emitter.on('foo', listener); hasListeners(emitter); // true hasListeners(emitter, 'foo'); // true hasListeners(emitter, 'bar'); // false emitter.off('foo', listener); hasListeners(emitter, 'foo'); // false ``` #### pipe(source, target[, emitMethodName]) _(event-emitter/pipe)_ Pipes all events from _source_ emitter onto _target_ emitter (all events from _source_ emitter will be emitted also on _target_ emitter, but not other way). Returns _pipe_ object which exposes `pipe.close` function. Invoke it to close configured _pipe_. It works internally by redefinition of `emit` method, if in your interface this method is referenced differently, provide its name (or symbol) with third argument. #### unify(emitter1, emitter2) _(event-emitter/unify)_ Unifies event handling for two objects. Events emitted on _emitter1_ would be also emitted on _emitter2_, and other way back. Non reversible. ```javascript var eeUnify = require('event-emitter/unify'); var emitter1 = ee(), listener1, listener3; var emitter2 = ee(), listener2, listener4; emitter1.on('test', listener1 = function () { }); emitter2.on('test', listener2 = function () { }); emitter1.emit('test'); // Invoked listener1 emitter2.emit('test'); // Invoked listener2 var unify = eeUnify(emitter1, emitter2); emitter1.emit('test'); // Invoked listener1 and listener2 emitter2.emit('test'); // Invoked listener1 and listener2 emitter1.on('test', listener3 = function () { }); emitter2.on('test', listener4 = function () { }); emitter1.emit('test'); // Invoked listener1, listener2, listener3 and listener4 emitter2.emit('test'); // Invoked listener1, listener2, listener3 and listener4 ``` ### Tests [![Build Status](https://travis-ci.org/medikoo/event-emitter.png)](https://travis-ci.org/medikoo/event-emitter) $ npm test event-emitter-0.3.5/all-off.js000066400000000000000000000007111306225506600161710ustar00rootroot00000000000000'use strict'; var value = require('es5-ext/object/valid-object') , hasOwnProperty = Object.prototype.hasOwnProperty; module.exports = function (emitter/*, type*/) { var type = arguments[1], data; value(emitter); if (type !== undefined) { data = hasOwnProperty.call(emitter, '__ee__') && emitter.__ee__; if (!data) return; if (data[type]) delete data[type]; return; } if (hasOwnProperty.call(emitter, '__ee__')) delete emitter.__ee__; }; event-emitter-0.3.5/benchmark/000077500000000000000000000000001306225506600162465ustar00rootroot00000000000000event-emitter-0.3.5/benchmark/many-on.js000066400000000000000000000040371306225506600201660ustar00rootroot00000000000000'use strict'; // Benchmark comparing performance of event emit for many listeners // To run it, do following in memoizee package path: // // $ npm install eventemitter2 signals // $ node benchmark/many-on.js var forEach = require('es5-ext/object/for-each') , pad = require('es5-ext/string/#/pad') , now = Date.now , time, count = 1000000, i, data = {} , ee, native, ee2, signals, a = {}, b = {}; ee = (function () { var ee = require('../')(); ee.on('test', function () { return arguments; }); ee.on('test', function () { return arguments; }); return ee.on('test', function () { return arguments; }); }()); native = (function () { var ee = require('events'); ee = new ee.EventEmitter(); ee.on('test', function () { return arguments; }); ee.on('test', function () { return arguments; }); return ee.on('test', function () { return arguments; }); }()); ee2 = (function () { var ee = require('eventemitter2'); ee = new ee.EventEmitter2(); ee.on('test', function () { return arguments; }); ee.on('test', function () { return arguments; }); return ee.on('test', function () { return arguments; }); }()); signals = (function () { var Signal = require('signals') , ee = { test: new Signal() }; ee.test.add(function () { return arguments; }); ee.test.add(function () { return arguments; }); ee.test.add(function () { return arguments; }); return ee; }()); console.log("Emit for 3 listeners", "x" + count + ":\n"); i = count; time = now(); while (i--) { ee.emit('test', a, b); } data["event-emitter (this implementation)"] = now() - time; i = count; time = now(); while (i--) { native.emit('test', a, b); } data["EventEmitter (Node.js native)"] = now() - time; i = count; time = now(); while (i--) { ee2.emit('test', a, b); } data.EventEmitter2 = now() - time; i = count; time = now(); while (i--) { signals.test.dispatch(a, b); } data.Signals = now() - time; forEach(data, function (value, name, obj, index) { console.log(index + 1 + ":", pad.call(value, " ", 5), name); }, null, function (a, b) { return this[a] - this[b]; }); event-emitter-0.3.5/benchmark/single-on.js000066400000000000000000000032021306225506600204740ustar00rootroot00000000000000'use strict'; // Benchmark comparing performance of event emit for single listener // To run it, do following in memoizee package path: // // $ npm install eventemitter2 signals // $ node benchmark/single-on.js var forEach = require('es5-ext/object/for-each') , pad = require('es5-ext/string/#/pad') , now = Date.now , time, count = 1000000, i, data = {} , ee, native, ee2, signals, a = {}, b = {}; ee = (function () { var ee = require('../'); return ee().on('test', function () { return arguments; }); }()); native = (function () { var ee = require('events'); return (new ee.EventEmitter()).on('test', function () { return arguments; }); }()); ee2 = (function () { var ee = require('eventemitter2'); return (new ee.EventEmitter2()).on('test', function () { return arguments; }); }()); signals = (function () { var Signal = require('signals') , ee = { test: new Signal() }; ee.test.add(function () { return arguments; }); return ee; }()); console.log("Emit for single listener", "x" + count + ":\n"); i = count; time = now(); while (i--) { ee.emit('test', a, b); } data["event-emitter (this implementation)"] = now() - time; i = count; time = now(); while (i--) { native.emit('test', a, b); } data["EventEmitter (Node.js native)"] = now() - time; i = count; time = now(); while (i--) { ee2.emit('test', a, b); } data.EventEmitter2 = now() - time; i = count; time = now(); while (i--) { signals.test.dispatch(a, b); } data.Signals = now() - time; forEach(data, function (value, name, obj, index) { console.log(index + 1 + ":", pad.call(value, " ", 5), name); }, null, function (a, b) { return this[a] - this[b]; }); event-emitter-0.3.5/emit-error.js000066400000000000000000000006131306225506600167370ustar00rootroot00000000000000'use strict'; var ensureError = require('es5-ext/error/valid-error') , ensureObject = require('es5-ext/object/valid-object') , hasOwnProperty = Object.prototype.hasOwnProperty; module.exports = function (err) { (ensureObject(this) && ensureError(err)); if (!hasOwnProperty.call(ensureObject(this), '__ee__')) throw err; if (!this.__ee__.error) throw err; this.emit('error', err); }; event-emitter-0.3.5/has-listeners.js000066400000000000000000000006611306225506600174360ustar00rootroot00000000000000'use strict'; var isEmpty = require('es5-ext/object/is-empty') , value = require('es5-ext/object/valid-value') , hasOwnProperty = Object.prototype.hasOwnProperty; module.exports = function (obj/*, type*/) { var type; value(obj); type = arguments[1]; if (arguments.length > 1) { return hasOwnProperty.call(obj, '__ee__') && Boolean(obj.__ee__[type]); } return obj.hasOwnProperty('__ee__') && !isEmpty(obj.__ee__); }; event-emitter-0.3.5/index.js000066400000000000000000000056701306225506600157710ustar00rootroot00000000000000'use strict'; var d = require('d') , callable = require('es5-ext/object/valid-callable') , apply = Function.prototype.apply, call = Function.prototype.call , create = Object.create, defineProperty = Object.defineProperty , defineProperties = Object.defineProperties , hasOwnProperty = Object.prototype.hasOwnProperty , descriptor = { configurable: true, enumerable: false, writable: true } , on, once, off, emit, methods, descriptors, base; on = function (type, listener) { var data; callable(listener); if (!hasOwnProperty.call(this, '__ee__')) { data = descriptor.value = create(null); defineProperty(this, '__ee__', descriptor); descriptor.value = null; } else { data = this.__ee__; } if (!data[type]) data[type] = listener; else if (typeof data[type] === 'object') data[type].push(listener); else data[type] = [data[type], listener]; return this; }; once = function (type, listener) { var once, self; callable(listener); self = this; on.call(this, type, once = function () { off.call(self, type, once); apply.call(listener, this, arguments); }); once.__eeOnceListener__ = listener; return this; }; off = function (type, listener) { var data, listeners, candidate, i; callable(listener); if (!hasOwnProperty.call(this, '__ee__')) return this; data = this.__ee__; if (!data[type]) return this; listeners = data[type]; if (typeof listeners === 'object') { for (i = 0; (candidate = listeners[i]); ++i) { if ((candidate === listener) || (candidate.__eeOnceListener__ === listener)) { if (listeners.length === 2) data[type] = listeners[i ? 0 : 1]; else listeners.splice(i, 1); } } } else { if ((listeners === listener) || (listeners.__eeOnceListener__ === listener)) { delete data[type]; } } return this; }; emit = function (type) { var i, l, listener, listeners, args; if (!hasOwnProperty.call(this, '__ee__')) return; listeners = this.__ee__[type]; if (!listeners) return; if (typeof listeners === 'object') { l = arguments.length; args = new Array(l - 1); for (i = 1; i < l; ++i) args[i - 1] = arguments[i]; listeners = listeners.slice(); for (i = 0; (listener = listeners[i]); ++i) { apply.call(listener, this, args); } } else { switch (arguments.length) { case 1: call.call(listeners, this); break; case 2: call.call(listeners, this, arguments[1]); break; case 3: call.call(listeners, this, arguments[1], arguments[2]); break; default: l = arguments.length; args = new Array(l - 1); for (i = 1; i < l; ++i) { args[i - 1] = arguments[i]; } apply.call(listeners, this, args); } } }; methods = { on: on, once: once, off: off, emit: emit }; descriptors = { on: d(on), once: d(once), off: d(off), emit: d(emit) }; base = defineProperties({}, descriptors); module.exports = exports = function (o) { return (o == null) ? create(base) : defineProperties(Object(o), descriptors); }; exports.methods = methods; event-emitter-0.3.5/package.json000066400000000000000000000016101306225506600166000ustar00rootroot00000000000000{ "name": "event-emitter", "version": "0.3.5", "description": "Environment agnostic event emitter", "author": "Mariusz Nowak (http://www.medikoo.com/)", "keywords": [ "event", "events", "trigger", "observer", "listener", "emitter", "pubsub" ], "repository": { "type": "git", "url": "git://github.com/medikoo/event-emitter.git" }, "dependencies": { "es5-ext": "~0.10.14", "d": "1" }, "devDependencies": { "tad": "~0.2.7", "xlint": "~0.2.2", "xlint-jslint-medikoo": "~0.1.4" }, "scripts": { "lint": "node node_modules/xlint/bin/xlint --linter=node_modules/xlint-jslint-medikoo/index.js --no-cache --no-stream", "lint-console": "node node_modules/xlint/bin/xlint --linter=node_modules/xlint-jslint-medikoo/index.js --watch", "test": "node ./node_modules/tad/bin/tad" }, "license": "MIT" } event-emitter-0.3.5/pipe.js000066400000000000000000000022311306225506600156050ustar00rootroot00000000000000'use strict'; var aFrom = require('es5-ext/array/from') , remove = require('es5-ext/array/#/remove') , value = require('es5-ext/object/valid-object') , d = require('d') , emit = require('./').methods.emit , defineProperty = Object.defineProperty , hasOwnProperty = Object.prototype.hasOwnProperty , getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; module.exports = function (e1, e2/*, name*/) { var pipes, pipe, desc, name; (value(e1) && value(e2)); name = arguments[2]; if (name === undefined) name = 'emit'; pipe = { close: function () { remove.call(pipes, e2); } }; if (hasOwnProperty.call(e1, '__eePipes__')) { (pipes = e1.__eePipes__).push(e2); return pipe; } defineProperty(e1, '__eePipes__', d('c', pipes = [e2])); desc = getOwnPropertyDescriptor(e1, name); if (!desc) { desc = d('c', undefined); } else { delete desc.get; delete desc.set; } desc.value = function () { var i, emitter, data = aFrom(pipes); emit.apply(this, arguments); for (i = 0; (emitter = data[i]); ++i) emit.apply(emitter, arguments); }; defineProperty(e1, name, desc); return pipe; }; event-emitter-0.3.5/test/000077500000000000000000000000001306225506600152735ustar00rootroot00000000000000event-emitter-0.3.5/test/all-off.js000066400000000000000000000013401306225506600171470ustar00rootroot00000000000000'use strict'; var ee = require('../'); module.exports = function (t, a) { var x, count, count2; x = ee(); count = 0; count2 = 0; x.on('foo', function () { ++count; }); x.on('foo', function () { ++count; }); x.on('bar', function () { ++count2; }); x.on('bar', function () { ++count2; }); t(x, 'foo'); x.emit('foo'); x.emit('bar'); a(count, 0, "All off: type"); a(count2, 2, "All off: ohter type"); count = 0; count2 = 0; x.on('foo', function () { ++count; }); x.on('foo', function () { ++count; }); x.on('bar', function () { ++count2; }); x.on('bar', function () { ++count2; }); t(x); x.emit('foo'); x.emit('bar'); a(count, 0, "All off: type"); a(count2, 0, "All off: other type"); }; event-emitter-0.3.5/test/emit-error.js000066400000000000000000000005561306225506600177240ustar00rootroot00000000000000'use strict'; var customError = require('es5-ext/error/custom') , ee = require('../'); module.exports = function (t, a) { var x, error = customError('Some error', 'ERROR_TEST'), emitted; x = ee(); a.throws(function () { t.call(x, error); }, 'ERROR_TEST'); x.on('error', function (err) { emitted = err; }); t.call(x, error); a(emitted, error); }; event-emitter-0.3.5/test/has-listeners.js000066400000000000000000000024711306225506600204160ustar00rootroot00000000000000'use strict'; var ee = require('../'); module.exports = function (t) { var x, y; return { Any: function (a) { a(t(true), false, "Primitive"); a(t({ events: [] }), false, "Other object"); a(t(x = ee()), false, "Emitter: empty"); x.on('test', y = function () {}); a(t(x), true, "Emitter: full"); x.off('test', y); a(t(x), false, "Emitter: empty but touched"); x.once('test', y = function () {}); a(t(x), true, "Emitter: full: Once"); x.off('test', y); a(t(x), false, "Emitter: empty but touched by once"); }, Specific: function (a) { a(t(true, 'test'), false, "Primitive"); a(t({ events: [] }, 'test'), false, "Other object"); a(t(x = ee(), 'test'), false, "Emitter: empty"); x.on('test', y = function () {}); a(t(x, 'test'), true, "Emitter: full"); a(t(x, 'foo'), false, "Emitter: full, other event"); x.off('test', y); a(t(x, 'test'), false, "Emitter: empty but touched"); a(t(x, 'foo'), false, "Emitter: empty but touched, other event"); x.once('test', y = function () {}); a(t(x, 'test'), true, "Emitter: full: Once"); a(t(x, 'foo'), false, "Emitter: full: Once, other event"); x.off('test', y); a(t(x, 'test'), false, "Emitter: empty but touched by once"); a(t(x, 'foo'), false, "Emitter: empty but touched by once, other event"); } }; }; event-emitter-0.3.5/test/index.js000066400000000000000000000047261306225506600167510ustar00rootroot00000000000000'use strict'; module.exports = function (t, a) { var x = t(), y, count, count2, count3, count4, test, listener1, listener2; x.emit('none'); test = "Once: "; count = 0; x.once('foo', function (a1, a2, a3) { a(this, x, test + "Context"); a.deep([a1, a2, a3], ['foo', x, 15], test + "Arguments"); ++count; }); x.emit('foobar'); a(count, 0, test + "Not invoked on other event"); x.emit('foo', 'foo', x, 15); a(count, 1, test + "Emitted"); x.emit('foo'); a(count, 1, test + "Emitted once"); test = "On & Once: "; count = 0; x.on('foo', listener1 = function (a1, a2, a3) { a(this, x, test + "Context"); a.deep([a1, a2, a3], ['foo', x, 15], test + "Arguments"); ++count; }); count2 = 0; x.once('foo', listener2 = function (a1, a2, a3) { a(this, x, test + "Context"); a.deep([a1, a2, a3], ['foo', x, 15], test + "Arguments"); ++count2; }); x.emit('foobar'); a(count, 0, test + "Not invoked on other event"); x.emit('foo', 'foo', x, 15); a(count, 1, test + "Emitted"); x.emit('foo', 'foo', x, 15); a(count, 2, test + "Emitted twice"); a(count2, 1, test + "Emitted once"); x.off('foo', listener1); x.emit('foo'); a(count, 2, test + "Not emitter after off"); count = 0; x.once('foo', listener1 = function () { ++count; }); x.off('foo', listener1); x.emit('foo'); a(count, 0, "Once Off: Not emitted"); count = 0; x.on('foo', listener2 = function () {}); x.once('foo', listener1 = function () { ++count; }); x.off('foo', listener1); x.emit('foo'); a(count, 0, "Once Off (multi): Not emitted"); x.off('foo', listener2); test = "Prototype Share: "; y = Object.create(x); count = 0; count2 = 0; count3 = 0; count4 = 0; x.on('foo', function () { ++count; }); y.on('foo', function () { ++count2; }); x.once('foo', function () { ++count3; }); y.once('foo', function () { ++count4; }); x.emit('foo'); a(count, 1, test + "x on count"); a(count2, 0, test + "y on count"); a(count3, 1, test + "x once count"); a(count4, 0, test + "y once count"); y.emit('foo'); a(count, 1, test + "x on count"); a(count2, 1, test + "y on count"); a(count3, 1, test + "x once count"); a(count4, 1, test + "y once count"); x.emit('foo'); a(count, 2, test + "x on count"); a(count2, 1, test + "y on count"); a(count3, 1, test + "x once count"); a(count4, 1, test + "y once count"); y.emit('foo'); a(count, 2, test + "x on count"); a(count2, 2, test + "y on count"); a(count3, 1, test + "x once count"); a(count4, 1, test + "y once count"); }; event-emitter-0.3.5/test/pipe.js000066400000000000000000000017141306225506600165710ustar00rootroot00000000000000'use strict'; var ee = require('../'); module.exports = function (t, a) { var x = {}, y = {}, z = {}, count, count2, count3, pipe; ee(x); x = Object.create(x); ee(y); ee(z); count = 0; count2 = 0; count3 = 0; x.on('foo', function () { ++count; }); y.on('foo', function () { ++count2; }); z.on('foo', function () { ++count3; }); x.emit('foo'); a(count, 1, "Pre pipe, x"); a(count2, 0, "Pre pipe, y"); a(count3, 0, "Pre pipe, z"); pipe = t(x, y); x.emit('foo'); a(count, 2, "Post pipe, x"); a(count2, 1, "Post pipe, y"); a(count3, 0, "Post pipe, z"); y.emit('foo'); a(count, 2, "Post pipe, on y, x"); a(count2, 2, "Post pipe, on y, y"); a(count3, 0, "Post pipe, on y, z"); t(x, z); x.emit('foo'); a(count, 3, "Post pipe z, x"); a(count2, 3, "Post pipe z, y"); a(count3, 1, "Post pipe z, z"); pipe.close(); x.emit('foo'); a(count, 4, "Close pipe y, x"); a(count2, 3, "Close pipe y, y"); a(count3, 2, "Close pipe y, z"); }; event-emitter-0.3.5/test/unify.js000066400000000000000000000054051306225506600167670ustar00rootroot00000000000000'use strict'; var ee = require('../'); module.exports = function (t) { return { "": function (a) { var x = {}, y = {}, z = {}, count, count2, count3; ee(x); ee(y); ee(z); count = 0; count2 = 0; count3 = 0; x.on('foo', function () { ++count; }); y.on('foo', function () { ++count2; }); z.on('foo', function () { ++count3; }); x.emit('foo'); a(count, 1, "Pre unify, x"); a(count2, 0, "Pre unify, y"); a(count3, 0, "Pre unify, z"); t(x, y); a(x.__ee__, y.__ee__, "Post unify y"); x.emit('foo'); a(count, 2, "Post unify, x"); a(count2, 1, "Post unify, y"); a(count3, 0, "Post unify, z"); y.emit('foo'); a(count, 3, "Post unify, on y, x"); a(count2, 2, "Post unify, on y, y"); a(count3, 0, "Post unify, on y, z"); t(x, z); a(x.__ee__, x.__ee__, "Post unify z"); x.emit('foo'); a(count, 4, "Post unify z, x"); a(count2, 3, "Post unify z, y"); a(count3, 1, "Post unify z, z"); }, "On empty": function (a) { var x = {}, y = {}, z = {}, count, count2, count3; ee(x); ee(y); ee(z); count = 0; count2 = 0; count3 = 0; y.on('foo', function () { ++count2; }); x.emit('foo'); a(count, 0, "Pre unify, x"); a(count2, 0, "Pre unify, y"); a(count3, 0, "Pre unify, z"); t(x, y); a(x.__ee__, y.__ee__, "Post unify y"); x.on('foo', function () { ++count; }); x.emit('foo'); a(count, 1, "Post unify, x"); a(count2, 1, "Post unify, y"); a(count3, 0, "Post unify, z"); y.emit('foo'); a(count, 2, "Post unify, on y, x"); a(count2, 2, "Post unify, on y, y"); a(count3, 0, "Post unify, on y, z"); t(x, z); a(x.__ee__, z.__ee__, "Post unify z"); z.on('foo', function () { ++count3; }); x.emit('foo'); a(count, 3, "Post unify z, x"); a(count2, 3, "Post unify z, y"); a(count3, 1, "Post unify z, z"); }, Many: function (a) { var x = {}, y = {}, z = {}, count, count2, count3; ee(x); ee(y); ee(z); count = 0; count2 = 0; count3 = 0; x.on('foo', function () { ++count; }); y.on('foo', function () { ++count2; }); y.on('foo', function () { ++count2; }); z.on('foo', function () { ++count3; }); x.emit('foo'); a(count, 1, "Pre unify, x"); a(count2, 0, "Pre unify, y"); a(count3, 0, "Pre unify, z"); t(x, y); a(x.__ee__, y.__ee__, "Post unify y"); x.emit('foo'); a(count, 2, "Post unify, x"); a(count2, 2, "Post unify, y"); a(count3, 0, "Post unify, z"); y.emit('foo'); a(count, 3, "Post unify, on y, x"); a(count2, 4, "Post unify, on y, y"); a(count3, 0, "Post unify, on y, z"); t(x, z); a(x.__ee__, x.__ee__, "Post unify z"); x.emit('foo'); a(count, 4, "Post unify z, x"); a(count2, 6, "Post unify z, y"); a(count3, 1, "Post unify z, z"); } }; }; event-emitter-0.3.5/unify.js000066400000000000000000000025451306225506600160120ustar00rootroot00000000000000'use strict'; var forEach = require('es5-ext/object/for-each') , validValue = require('es5-ext/object/valid-object') , push = Array.prototype.apply, defineProperty = Object.defineProperty , create = Object.create, hasOwnProperty = Object.prototype.hasOwnProperty , d = { configurable: true, enumerable: false, writable: true }; module.exports = function (e1, e2) { var data; (validValue(e1) && validValue(e2)); if (!hasOwnProperty.call(e1, '__ee__')) { if (!hasOwnProperty.call(e2, '__ee__')) { d.value = create(null); defineProperty(e1, '__ee__', d); defineProperty(e2, '__ee__', d); d.value = null; return; } d.value = e2.__ee__; defineProperty(e1, '__ee__', d); d.value = null; return; } data = d.value = e1.__ee__; if (!hasOwnProperty.call(e2, '__ee__')) { defineProperty(e2, '__ee__', d); d.value = null; return; } if (data === e2.__ee__) return; forEach(e2.__ee__, function (listener, name) { if (!data[name]) { data[name] = listener; return; } if (typeof data[name] === 'object') { if (typeof listener === 'object') push.apply(data[name], listener); else data[name].push(listener); } else if (typeof listener === 'object') { listener.unshift(data[name]); data[name] = listener; } else { data[name] = [data[name], listener]; } }); defineProperty(e2, '__ee__', d); d.value = null; };