root / HServer / 00.Server / 00.Program / node_modules / ultron / index.js
이력 | 보기 | 이력해설 | 다운로드 (3.04 KB)
| 1 | 39 | HKM | 'use strict';
|
|---|---|---|---|
| 2 | |||
| 3 | var has = Object.prototype.hasOwnProperty;
|
||
| 4 | |||
| 5 | /**
|
||
| 6 | * An auto incrementing id which we can use to create "unique" Ultron instances
|
||
| 7 | * so we can track the event emitters that are added through the Ultron
|
||
| 8 | * interface.
|
||
| 9 | *
|
||
| 10 | * @type {Number}
|
||
| 11 | * @private
|
||
| 12 | */
|
||
| 13 | var id = 0; |
||
| 14 | |||
| 15 | /**
|
||
| 16 | * Ultron is high-intelligence robot. It gathers intelligence so it can start improving
|
||
| 17 | * upon his rudimentary design. It will learn from your EventEmitting patterns
|
||
| 18 | * and exterminate them.
|
||
| 19 | *
|
||
| 20 | * @constructor
|
||
| 21 | * @param {EventEmitter} ee EventEmitter instance we need to wrap.
|
||
| 22 | * @api public
|
||
| 23 | */
|
||
| 24 | function Ultron(ee) { |
||
| 25 | if (!(this instanceof Ultron)) return new Ultron(ee); |
||
| 26 | |||
| 27 | this.id = id++;
|
||
| 28 | this.ee = ee;
|
||
| 29 | } |
||
| 30 | |||
| 31 | /**
|
||
| 32 | * Register a new EventListener for the given event.
|
||
| 33 | *
|
||
| 34 | * @param {String} event Name of the event.
|
||
| 35 | * @param {Functon} fn Callback function.
|
||
| 36 | * @param {Mixed} context The context of the function.
|
||
| 37 | * @returns {Ultron}
|
||
| 38 | * @api public
|
||
| 39 | */
|
||
| 40 | Ultron.prototype.on = function on(event, fn, context) { |
||
| 41 | fn.__ultron = this.id;
|
||
| 42 | this.ee.on(event, fn, context);
|
||
| 43 | |||
| 44 | return this; |
||
| 45 | }; |
||
| 46 | /**
|
||
| 47 | * Add an EventListener that's only called once.
|
||
| 48 | *
|
||
| 49 | * @param {String} event Name of the event.
|
||
| 50 | * @param {Function} fn Callback function.
|
||
| 51 | * @param {Mixed} context The context of the function.
|
||
| 52 | * @returns {Ultron}
|
||
| 53 | * @api public
|
||
| 54 | */
|
||
| 55 | Ultron.prototype.once = function once(event, fn, context) { |
||
| 56 | fn.__ultron = this.id;
|
||
| 57 | this.ee.once(event, fn, context);
|
||
| 58 | |||
| 59 | return this; |
||
| 60 | }; |
||
| 61 | |||
| 62 | /**
|
||
| 63 | * Remove the listeners we assigned for the given event.
|
||
| 64 | *
|
||
| 65 | * @returns {Ultron}
|
||
| 66 | * @api public
|
||
| 67 | */
|
||
| 68 | Ultron.prototype.remove = function remove() { |
||
| 69 | var args = arguments |
||
| 70 | , ee = this.ee
|
||
| 71 | , event; |
||
| 72 | |||
| 73 | //
|
||
| 74 | // When no event names are provided we assume that we need to clear all the
|
||
| 75 | // events that were assigned through us.
|
||
| 76 | //
|
||
| 77 | if (args.length === 1 && 'string' === typeof args[0]) { |
||
| 78 | args = args[0].split(/[, ]+/); |
||
| 79 | } else if (!args.length) { |
||
| 80 | if (ee.eventNames) {
|
||
| 81 | args = ee.eventNames(); |
||
| 82 | } else if (ee._events) { |
||
| 83 | args = []; |
||
| 84 | |||
| 85 | for (event in ee._events) { |
||
| 86 | if (has.call(ee._events, event)) args.push(event);
|
||
| 87 | } |
||
| 88 | |||
| 89 | if (Object.getOwnPropertySymbols) {
|
||
| 90 | args = args.concat(Object.getOwnPropertySymbols(ee._events)); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | for (var i = 0; i < args.length; i++) { |
||
| 96 | var listeners = ee.listeners(args[i]);
|
||
| 97 | |||
| 98 | for (var j = 0; j < listeners.length; j++) { |
||
| 99 | event = listeners[j]; |
||
| 100 | |||
| 101 | //
|
||
| 102 | // Once listeners have a `listener` property that stores the real listener
|
||
| 103 | // in the EventEmitter that ships with Node.js.
|
||
| 104 | //
|
||
| 105 | if (event.listener) {
|
||
| 106 | if (event.listener.__ultron !== this.id) continue; |
||
| 107 | } else if (event.__ultron !== this.id) { |
||
| 108 | continue;
|
||
| 109 | } |
||
| 110 | |||
| 111 | ee.removeListener(args[i], event); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return this; |
||
| 116 | }; |
||
| 117 | |||
| 118 | /**
|
||
| 119 | * Destroy the Ultron instance, remove all listeners and release all references.
|
||
| 120 | *
|
||
| 121 | * @returns {Boolean}
|
||
| 122 | * @api public
|
||
| 123 | */
|
||
| 124 | Ultron.prototype.destroy = function destroy() { |
||
| 125 | if (!this.ee) return false; |
||
| 126 | |||
| 127 | this.remove();
|
||
| 128 | this.ee = null; |
||
| 129 | |||
| 130 | return true; |
||
| 131 | }; |
||
| 132 | |||
| 133 | //
|
||
| 134 | // Expose the module.
|
||
| 135 | //
|
||
| 136 | module.exports = Ultron; |