aboutsummaryrefslogtreecommitdiff
path: root/node_modules/ultron/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-11-03 01:33:53 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-11-03 01:33:53 +0100
commitd1291f67551c58168af43698a359cb5ddfd266b0 (patch)
tree55a13ed29fe1915e3f42f1b1b7038dafa2e975a7 /node_modules/ultron/index.js
parentd0a0695fb5d34996850723f7d4b1b59c3df909c2 (diff)
node_modules
Diffstat (limited to 'node_modules/ultron/index.js')
-rw-r--r--node_modules/ultron/index.js129
1 files changed, 129 insertions, 0 deletions
diff --git a/node_modules/ultron/index.js b/node_modules/ultron/index.js
new file mode 100644
index 000000000..af17ab7cc
--- /dev/null
+++ b/node_modules/ultron/index.js
@@ -0,0 +1,129 @@
+'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
+ , 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) {
+ args = [];
+
+ for (event in this.ee._events) {
+ if (has.call(this.ee._events, event)) args.push(event);
+ }
+ }
+
+ for (var i = 0; i < args.length; i++) {
+ var listeners = this.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;
+ delete event.listener.__ultron;
+ } else {
+ if (event.__ultron !== this.id) continue;
+ delete event.__ultron;
+ }
+
+ this.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;