fair-twitch
Version:
Fair's Twitch API and Chat bot library
108 lines (107 loc) • 4.19 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var events_1 = __importDefault(require("events"));
var ExpandedEventEmitter = /** @class */ (function (_super) {
__extends(ExpandedEventEmitter, _super);
function ExpandedEventEmitter() {
var _this = _super.call(this) || this;
_this.idListeners = [];
_super.prototype.on.call(_this, 'removeListener', function (event, listener) {
for (var i = 0; i < _this.idListeners.length; i++) {
var data = _this.idListeners[i];
if (data.event !== event)
continue;
if (data.listener !== listener)
continue;
_this.idListeners.splice(i, 1);
i--;
}
});
return _this;
}
ExpandedEventEmitter.prototype.on = function (event, listener) {
var data = this._splitID(event, listener);
_super.prototype.on.call(this, data.event, listener);
if (data.id !== null)
this.idListeners.push(data);
return this;
};
ExpandedEventEmitter.prototype.addListener = function (event, listener) {
return this.on(event, listener);
};
ExpandedEventEmitter.prototype.once = function (event, listener) {
var data = this._splitID(event, listener);
_super.prototype.once.call(this, data.event, listener);
if (data.id !== null)
this.idListeners.push(data);
return this;
};
ExpandedEventEmitter.prototype.prependListener = function (event, listener) {
var data = this._splitID(event, listener);
_super.prototype.prependListener.call(this, data.event, listener);
if (data.id !== null)
this.idListeners.push(data);
return this;
};
ExpandedEventEmitter.prototype.prependOnceListener = function (event, listener) {
var data = this._splitID(event, listener);
_super.prototype.prependOnceListener.call(this, data.event, listener);
this.idListeners.push(data);
this.listeners;
return this;
};
ExpandedEventEmitter.prototype.getIDListenersData = function (id, event) {
var out = [];
for (var i = 0; i < this.idListeners.length; i++) {
var data = this.idListeners[i];
if (data.id !== id)
continue;
if (event !== undefined && event !== data.event)
continue;
out.push(data);
}
return out;
};
ExpandedEventEmitter.prototype.getIDListeners = function (id, event) {
return this.getIDListenersData(id, event).map(function (e) { return e.listener; });
};
ExpandedEventEmitter.prototype.removeIDListeners = function (id) {
var _this = this;
this.getIDListenersData(id).forEach(function (data) {
_this.removeListener(data.event, data.listener);
});
return this;
};
ExpandedEventEmitter.prototype._splitID = function (event, listener) {
var id = null;
if (typeof event === 'string') {
var index = event.indexOf('#');
if (index !== -1) {
id = event.substring(index + 1);
event = event.substring(0, index);
}
}
return {
event: event,
id: id,
listener: listener
};
};
return ExpandedEventEmitter;
}(events_1.default));
module.exports = ExpandedEventEmitter;