/usr/lib/nodejs/@sellside/emitter/index.js is in node-sellside-emitter 1.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | 'use strict';
/**
* Initialize a new `Emitter`.
*
* ```js
* const Emitter = require('emitter');
* // as an `Emitter` instance
* const emitter = new Emitter;
* emitter.emit('something');
* // or inherit
* class MyEmitter extends Emitter {}
* ```
* @name Emitter
* @api public
*/
class Emitter {
constructor(obj) {
if (obj) return mixin(obj);
}
/**
* Mixin methods from Emitter.
*
* ```js
* const Emitter = require('emitter');
* const obj = {};
* Emitter.mixin(obj);
* obj.on('status', console.log);
* obj.emit('status', 'I emit!');
* ```
* @name Emitter#mixin
* @param {Object} `obj`
* @return {Object}
* @api public
*/
static mixin(obj) {
return new Emitter(obj);
}
/**
* Return the array of registered listeners for `event`.
*
* ```js
* // all listeners for event "status"
* console.log(emitter.listeners('status'));
* // all listeners
* console.log(emitter.listeners());
* ```
* @name .listeners
* @param {String} `event`
* @return {Array}
* @api public
*/
listeners(event) {
if (!this._listeners) define(this, '_listeners', {});
if (!this._only) define(this, '_only', {});
if (!event) return this._listeners;
return this._listeners['$' + event] || (this._listeners['$' + event] = []);
}
/**
* Listen on the given `event` with `fn`.
*
* ```js
* emitter.on('foo', () => 'do stuff');
* ```
* @name .on
* @param {String} `event`
* @param {Function} `fn`
* @return {Emitter}
* @api public
*/
on(event, fn) {
if (this._only && this._only[event]) {
return this.only(event, fn);
}
this.listeners(event).push(fn);
return this;
}
/**
* Adds an `event` listener that will be invoked a single
* time then automatically removed.
*
* ```js
* emitter.only('once', () => 'do stuff');
* ```
* @name .once
* @param {String} `event`
* @param {Function} `fn`
* @return {Emitter}
* @api public
*/
once(event, fn) {
const on = function() {
this.off(event, on);
fn.apply(this, arguments);
};
on.fn = fn;
this.on(event, on);
return this;
}
/**
* Ensures that listeners for `event` are only **_registered_** once
* and are disabled correctly when specified. This is different from
* `.once`, which only **emits** once.
*
* ```js
* emitter.only('foo', () => 'do stuff');
* ```
* @name .only
* @param {String} `event`
* @param {Object} `options`
* @param {Function} `fn`
* @return {Emitter}
* @api public
*/
only(event, options, fn) {
this.listeners();
if (typeof options === 'function') {
fn = options;
options = null;
}
if (options && options.first === true) {
define(this, '_first', true);
}
if (!fn || !event || !this._only[event]) {
this.off(event);
if (!fn) return this;
}
const existing = this._only[event];
if (existing) {
if (this._first === true) return this;
this.off(event, existing);
}
this._only[event] = fn;
this.listeners(event).push(fn);
return this;
}
/**
* Remove the given listener for `event`, or remove all
* registered listeners if `event` is undefined.
*
* ```js
* emitter.off();
* emitter.off('foo');
* emitter.off('foo', fn);
* ```
* @name .off
* @param {String} `event`
* @param {Function} `fn`
* @return {Emitter}
* @api public
*/
off(event, fn) {
this.listeners();
// remove all listeners
if (!event) {
this._listeners = {};
this._only = {};
return this;
}
// remove all listeners for "event"
if (!fn) {
this._listeners['$' + event] = [];
this._only['$' + event] = [];
return this;
}
// remove all instances of "fn" from "event"
removeListeners(fn, this.listeners(event));
return this;
}
/**
* Emit `event` with the given args.
*
* ```js
* emitter.emit('foo', 'bar');
* ```
* @name .emit
* @param {String} `event`
* @param {Mixed} ...
* @return {Emitter}
*/
emit(event) {
const listeners = this.listeners(event).slice();
const args = [].slice.call(arguments, 1);
for (const fn of listeners) {
fn.apply(this, args);
}
return this;
}
/**
* Returns true if the emitter has registered listeners for `event`.
*
* ```js
* emitter.on('foo', 'do stuff');
* console.log(emitter.has('foo')); // true
* console.log(emitter.has('bar')); // false
* ```
* @name .has
* @param {String} `event`
* @return {Boolean}
* @api public
*/
has(event) {
return this.listeners(event).length > 0;
}
}
/**
* Expose common aliases for `.has`, `.on` and `.off`
*/
Emitter.prototype.hasListeners =
Emitter.prototype.has;
Emitter.prototype.addListener =
Emitter.prototype.addEventListener =
Emitter.prototype.on;
Emitter.prototype.removeListener =
Emitter.prototype.removeListeners =
Emitter.prototype.removeAllListeners =
Emitter.prototype.removeEventListener =
Emitter.prototype.off;
/**
* Remove all instances of the given `fn` from listeners.
*/
function removeListeners(fn, listeners) {
for (let i = 0; i < listeners.length; i++) {
const listener = listeners[i];
if (listener === fn || listener.fn === fn) {
listeners.splice(i, 1);
return removeListeners(fn, listeners);
}
}
}
/**
* Mixin emitter properties.
*/
function mixin(obj) {
const ctor = obj.constructor;
Object.setPrototypeOf(obj, Emitter.prototype);
if (ctor) define(obj, 'constructor', ctor);
return obj;
}
function define(obj, key, val) {
Reflect.defineProperty(obj, key, {
configurable: true,
writable: true,
value: val
});
}
/**
* Expose `Emitter`
*/
module.exports = Emitter;
|