pax_global_header00006660000000000000000000000064132053126410014507gustar00rootroot0000000000000052 comment=f7f3227ef2505cf9e7c3612b96e9eb43f42a9e2a ultron-1.1.1/000077500000000000000000000000001320531264100130325ustar00rootroot00000000000000ultron-1.1.1/.gitignore000066400000000000000000000000411320531264100150150ustar00rootroot00000000000000node_modules coverage .tern-port ultron-1.1.1/.npmignore000066400000000000000000000000251320531264100150260ustar00rootroot00000000000000coverage/ test.js .* ultron-1.1.1/.travis.yml000066400000000000000000000005041320531264100151420ustar00rootroot00000000000000sudo: false language: node_js node_js: - "8" - "6" - "4" script: - "npm run test-travis" after_script: - "npm install coveralls@3 && cat coverage/lcov.info | coveralls" matrix: fast_finish: true notifications: irc: channels: - "irc.freenode.org#unshift" on_success: change on_failure: change ultron-1.1.1/LICENSE000066400000000000000000000021331320531264100140360ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Unshift.io, Arnout Kazemier, the Contributors. 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. ultron-1.1.1/README.md000066400000000000000000000076561320531264100143270ustar00rootroot00000000000000# Ultron [![Made by unshift](https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square)](http://unshift.io)[![Version npm](http://img.shields.io/npm/v/ultron.svg?style=flat-square)](http://browsenpm.org/package/ultron)[![Build Status](http://img.shields.io/travis/unshiftio/ultron/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/ultron)[![Dependencies](https://img.shields.io/david/unshiftio/ultron.svg?style=flat-square)](https://david-dm.org/unshiftio/ultron)[![Coverage Status](http://img.shields.io/coveralls/unshiftio/ultron/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/ultron?branch=master)[![IRC channel](http://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square)](http://webchat.freenode.net/?channels=unshift) Ultron is a high-intelligence robot. It gathers intelligence so it can start improving upon his rudimentary design. It will learn your event emitting patterns and find ways to exterminate them. Allowing you to remove only the event emitters that **you** assigned and not the ones that your users or developers assigned. This can prevent race conditions, memory leaks and even file descriptor leaks from ever happening as you won't remove clean up processes. ## Installation The module is designed to be used in browsers using browserify and in Node.js. You can install the module through the public npm registry by running the following command in CLI: ``` npm install --save ultron ``` ## Usage In all examples we assume that you've required the library as following: ```js 'use strict'; var Ultron = require('ultron'); ``` Now that we've required the library we can construct our first `Ultron` instance. The constructor requires one argument which should be the `EventEmitter` instance that we need to operate upon. This can be the `EventEmitter` module that ships with Node.js or `EventEmitter3` or anything else as long as it follow the same API and internal structure as these 2. So with that in mind we can create the instance: ```js // // For the sake of this example we're going to construct an empty EventEmitter // var EventEmitter = require('events').EventEmitter; // or require('eventmitter3'); var events = new EventEmitter(); var ultron = new Ultron(events); ``` You can now use the following API's from the Ultron instance: ### Ultron.on Register a new event listener for the given event. It follows the exact same API as `EventEmitter.on` but it will return itself instead of returning the EventEmitter instance. If you are using EventEmitter3 it also supports the context param: ```js ultron.on('event-name', handler, { custom: 'function context' }); ``` Just like you would expect, it can also be chained together. ```js ultron .on('event-name', handler) .on('another event', handler); ``` ### Ultron.once Exactly the same as the [Ultron.on](#ultronon) but it only allows the execution once. Just like you would expect, it can also be chained together. ```js ultron .once('event-name', handler, { custom: 'this value' }) .once('another event', handler); ``` ### Ultron.remove This is where all the magic happens and the safe removal starts. This function accepts different argument styles: - No arguments, assume that all events need to be removed so it will work as `removeAllListeners()` API. - 1 argument, when it's a string it will be split on ` ` and `,` to create a list of events that need to be cleared. - Multiple arguments, we assume that they are all names of events that need to be cleared. ```js ultron.remove('foo, bar baz'); // Removes foo, bar and baz. ultron.remove('foo', 'bar', 'baz'); // Removes foo, bar and baz. ultron.remove(); // Removes everything. ``` If you just want to remove a single event listener using a function reference you can still use the EventEmitter's `removeListener(event, fn)` API: ```js function foo() {} ultron.on('foo', foo); events.removeListener('foo', foo); ``` ## License MIT ultron-1.1.1/index.js000066400000000000000000000060451320531264100145040ustar00rootroot00000000000000'use strict'; var has = Object.prototype.hasOwnProperty; /** * An auto incrementing id which we can use to create "unique" Ultron instances * so we can track the event emitters that are added through the Ultron * interface. * * @type {Number} * @private */ var id = 0; /** * Ultron is high-intelligence robot. It gathers intelligence so it can start improving * upon his rudimentary design. It will learn from your EventEmitting patterns * and exterminate them. * * @constructor * @param {EventEmitter} ee EventEmitter instance we need to wrap. * @api public */ function Ultron(ee) { if (!(this instanceof Ultron)) return new Ultron(ee); this.id = id++; this.ee = ee; } /** * Register a new EventListener for the given event. * * @param {String} event Name of the event. * @param {Functon} fn Callback function. * @param {Mixed} context The context of the function. * @returns {Ultron} * @api public */ Ultron.prototype.on = function on(event, fn, context) { fn.__ultron = this.id; this.ee.on(event, fn, context); return this; }; /** * Add an EventListener that's only called once. * * @param {String} event Name of the event. * @param {Function} fn Callback function. * @param {Mixed} context The context of the function. * @returns {Ultron} * @api public */ Ultron.prototype.once = function once(event, fn, context) { fn.__ultron = this.id; this.ee.once(event, fn, context); return this; }; /** * Remove the listeners we assigned for the given event. * * @returns {Ultron} * @api public */ Ultron.prototype.remove = function remove() { var args = arguments , ee = this.ee , event; // // When no event names are provided we assume that we need to clear all the // events that were assigned through us. // if (args.length === 1 && 'string' === typeof args[0]) { args = args[0].split(/[, ]+/); } else if (!args.length) { if (ee.eventNames) { args = ee.eventNames(); } else if (ee._events) { args = []; for (event in ee._events) { if (has.call(ee._events, event)) args.push(event); } if (Object.getOwnPropertySymbols) { args = args.concat(Object.getOwnPropertySymbols(ee._events)); } } } for (var i = 0; i < args.length; i++) { var listeners = ee.listeners(args[i]); for (var j = 0; j < listeners.length; j++) { event = listeners[j]; // // Once listeners have a `listener` property that stores the real listener // in the EventEmitter that ships with Node.js. // if (event.listener) { if (event.listener.__ultron !== this.id) continue; } else if (event.__ultron !== this.id) { continue; } ee.removeListener(args[i], event); } } return this; }; /** * Destroy the Ultron instance, remove all listeners and release all references. * * @returns {Boolean} * @api public */ Ultron.prototype.destroy = function destroy() { if (!this.ee) return false; this.remove(); this.ee = null; return true; }; // // Expose the module. // module.exports = Ultron; ultron-1.1.1/package.json000066400000000000000000000020761320531264100153250ustar00rootroot00000000000000{ "name": "ultron", "version": "1.1.1", "description": "Ultron is high-intelligence robot. It gathers intel so it can start improving upon his rudimentary design", "main": "index.js", "scripts": { "100%": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100", "test": "mocha test.js", "watch": "mocha --watch test.js", "coverage": "istanbul cover _mocha -- test.js", "test-travis": "istanbul cover _mocha --report lcovonly -- test.js" }, "repository": { "type": "git", "url": "https://github.com/unshiftio/ultron" }, "keywords": [ "Ultron", "robot", "gather", "intelligence", "event", "events", "eventemitter", "emitter", "cleanup" ], "author": "Arnout Kazemier", "license": "MIT", "devDependencies": { "assume": "~1.5.0", "eventemitter3": "2.0.x", "istanbul": "0.4.x", "mocha": "~4.0.0", "pre-commit": "~1.2.0" }, "bugs": { "url": "https://github.com/unshiftio/ultron/issues" }, "homepage": "https://github.com/unshiftio/ultron" } ultron-1.1.1/test.js000066400000000000000000000235171320531264100143570ustar00rootroot00000000000000/* istanbul ignore next */ describe('Ultron', function () { 'use strict'; var EventEmitter = require('eventemitter3') , EE = require('events').EventEmitter , assume = require('assume') , Ultron = require('./') , ultron , ee; beforeEach(function () { ee = new EventEmitter(); ultron = new Ultron(ee); }); afterEach(function () { ultron.destroy(); ee.removeAllListeners(); }); it('is exposed as a function', function () { assume(Ultron).is.a('function'); }); it('can be initialized without the new keyword', function () { assume(Ultron(ee)).is.instanceOf(Ultron); }); it('assigns a unique id to every instance', function () { for (var i = 0; i < 100; i++) { assume(ultron.id).does.not.equal((new Ultron()).id); } }); it('allows removal through the event emitter', function () { function foo() {} function bar() {} ultron.on('foo', foo); ultron.once('foo', bar); assume(foo.__ultron).equals(ultron.id); assume(bar.__ultron).equals(ultron.id); assume(ee.listeners('foo').length).equals(2); ee.removeListener('foo', foo); assume(ee.listeners('foo').length).equals(1); ee.removeListener('foo', bar); assume(ee.listeners('foo').length).equals(0); }); describe('#on', function () { it('assigns a listener', function () { assume(ee.listeners('foo').length).equals(0); function foo() {} ultron.on('foo', foo); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('foo')[0]).equals(foo); }); it('tags the assigned function', function () { assume(ee.listeners('foo').length).equals(0); ultron.on('foo', function () {}); assume(ee.listeners('foo')[0].__ultron).equals(ultron.id); }); it('also passes in the context', function (next) { var context = 1313; ultron.on('foo', function (a, b, c) { assume(a).equals('a'); assume(b).equals('b'); assume(c).equals('c'); assume(this).equals(context); next(); }, context); ee.emit('foo', 'a', 'b', 'c'); }); it('works with regular eventemitters as well', function (next) { var ee = new EE() , ultron = new Ultron(ee); ultron.on('foo', function (a, b, c) { assume(a).equals('a'); assume(b).equals('b'); assume(c).equals('c'); next(); }); ee.emit('foo', 'a', 'b', 'c'); }); }); describe('#once', function () { it('assigns a listener', function () { assume(ee.listeners('foo').length).equals(0); function foo() {} ultron.once('foo', foo); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('foo')[0]).equals(foo); }); it('tags the assigned function', function () { assume(ee.listeners('foo').length).equals(0); ultron.once('foo', function () {}); assume(ee.listeners('foo')[0].__ultron).equals(ultron.id); }); it('also passes in the context', function (next) { var context = 1313; ultron.once('foo', function (a, b, c) { assume(a).equals('a'); assume(b).equals('b'); assume(c).equals('c'); assume(this).equals(context); next(); }, context); ee.emit('foo', 'a', 'b', 'c'); ee.emit('foo', 'a', 'b', 'c'); // Ensure that we don't double execute }); it('works with regular eventemitters as well', function (next) { var ee = new EE() , ultron = new Ultron(ee); ultron.once('foo', function (a, b, c) { assume(a).equals('a'); assume(b).equals('b'); assume(c).equals('c'); next(); }); ee.emit('foo', 'a', 'b', 'c'); ee.emit('foo', 'a', 'b', 'c'); // Ensure that we don't double execute }); }); describe('#remove', function () { it('removes only our assigned `on` listeners', function () { function foo() {} function bar() {} ee.on('foo', foo); ultron.on('foo', bar); assume(ee.listeners('foo').length).equals(2); ultron.remove('foo'); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('foo')[0]).equals(foo); }); it('removes only our assigned `once` listeners', function () { function foo() {} function bar() {} ee.once('foo', foo); ultron.once('foo', bar); assume(ee.listeners('foo').length).equals(2); ultron.remove('foo'); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('foo')[0]).equals(foo); ee.removeAllListeners(); ultron.destroy(); ee = new EE(); ultron = new Ultron(ee); ee.once('foo', foo); ultron.once('foo', bar); assume(ee.listeners('foo').length).equals(2); ultron.remove('foo'); assume(ee.listeners('foo').length).equals(1); var actual = ee.listeners('foo')[0]; // // In Node.js 8+ `EventEmitter#listeners()` returns the original listener // also for once listeners. // assume(actual.listener || actual).equals(foo); }); it('removes all assigned events if called without args', function () { function foo() {} function bar() {} assume(ultron.remove()).equals(ultron); ultron.on('foo', foo); ultron.on('bar', bar); ultron.on('baz', bar); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('bar').length).equals(1); assume(ee.listeners('baz').length).equals(1); ultron.remove(); assume(ee.listeners('foo').length).equals(0); assume(ee.listeners('bar').length).equals(0); assume(ee.listeners('baz').length).equals(0); ee.removeAllListeners(); ultron.destroy(); ee = new EE(); ultron = new Ultron(ee); assume(ultron.remove()).equals(ultron); ultron.on('foo', foo); ultron.on('bar', bar); ultron.on('baz', bar); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('bar').length).equals(1); assume(ee.listeners('baz').length).equals(1); ultron.remove(); assume(ee.listeners('foo').length).equals(0); assume(ee.listeners('bar').length).equals(0); assume(ee.listeners('baz').length).equals(0); }); it('removes multiple listeners based on args', function () { function foo() {} function bar() {} function baz() {} ultron.on('foo', foo); ultron.on('bar', bar); ultron.on('baz', baz); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('bar').length).equals(1); assume(ee.listeners('baz').length).equals(1); ultron.remove('foo', 'bar'); assume(ee.listeners('foo').length).equals(0); assume(ee.listeners('bar').length).equals(0); assume(ee.listeners('baz').length).equals(1); }); it('removes multiple listeners if first arg is separated string', function () { function foo() {} function bar() {} function baz() {} ultron.on('foo', foo); ultron.on('bar', bar); ultron.on('baz', baz); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('bar').length).equals(1); assume(ee.listeners('baz').length).equals(1); ultron.remove('foo, bar'); assume(ee.listeners('foo').length).equals(0); assume(ee.listeners('bar').length).equals(0); assume(ee.listeners('baz').length).equals(1); }); if ('undefined' !== typeof Symbol) it('works with ES6 symbols', function () { var s = Symbol('s'); function foo() {} function bar() {} function baz() {} ee.on(s, foo); ultron.on(s, bar); assume(ee.listeners(s).length).equals(2); ultron.remove(s); assume(ee.listeners(s).length).equals(1); assume(ee.listeners(s)[0]).equals(foo); ultron.once(s, bar); assume(ee.listeners(s).length).equals(2); ultron.remove(s); assume(ee.listeners(s).length).equals(1); assume(ee.listeners(s)[0]).equals(foo); ultron.on(s, bar); ultron.on(s, baz); assume(ee.listeners(s).length).equals(3); ultron.remove(); assume(ee.listeners(s).length).equals(1); assume(ee.listeners(s)[0]).equals(foo); ee.removeAllListeners(); ultron.destroy(); ee = new EE(); ultron = new Ultron(ee); ee.on(s, foo); ultron.on(s, bar); assume(ee.listeners(s).length).equals(2); ultron.remove(s); assume(ee.listeners(s).length).equals(1); assume(ee.listeners(s)[0]).equals(foo); ultron.once(s, bar); assume(ee.listeners(s).length).equals(2); ultron.remove(s); assume(ee.listeners(s).length).equals(1); assume(ee.listeners(s)[0]).equals(foo); ultron.on(s, bar); ultron.on(s, baz); assume(ee.listeners(s).length).equals(3); ultron.remove(); assume(ee.listeners(s).length).equals(1); assume(ee.listeners(s)[0]).equals(foo); }); }); describe('#destroy', function () { it('removes all listeners', function () { function foo() {} function bar() {} function baz() {} ultron.on('foo', foo); ultron.on('bar', bar); ultron.on('baz', baz); assume(ee.listeners('foo').length).equals(1); assume(ee.listeners('bar').length).equals(1); assume(ee.listeners('baz').length).equals(1); ultron.destroy(); assume(ee.listeners('foo').length).equals(0); assume(ee.listeners('bar').length).equals(0); assume(ee.listeners('baz').length).equals(0); }); it('removes the .ee reference', function () { assume(ultron.ee).equals(ee); ultron.destroy(); assume(ultron.ee).equals(null); }); it('returns booleans for state indication', function () { assume(ultron.destroy()).is.true(); assume(ultron.destroy()).is.false(); assume(ultron.destroy()).is.false(); assume(ultron.destroy()).is.false(); }); }); });