tesjs
Version:
A module to streamline the use of Twitch EventSub in Node.js and Web applications
79 lines (66 loc) • 1.98 kB
JavaScript
// Copyright (c) 2020 Mitchell Adair
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
const logger = require("./logger");
class EventManager {
constructor() {
EventManager._instance = this;
this._events = this._events || {};
this._subscriptionQueue = this._subscriptionQueue || {};
}
fire(sub, data) {
const handler = this._events[sub.type];
if (!handler) {
logger.warn(`Recieved event for unhandled type: ${sub.type}`);
return false;
} else {
handler.call(this, data, sub);
return true;
}
}
addListener(type, handler) {
if (typeof handler !== "function") {
throw TypeError("Event handler must be a function");
}
this._events[type] = handler;
return this;
}
removeListener(type) {
if (this._events[type]) {
delete this._events[type];
}
return this;
}
removeAllListeners() {
this._events = {};
return this;
}
queueSubscription(data, resolve, reject) {
const id = data.data[0].id;
this._subscriptionQueue[id] = {
data,
resolve,
timeout: setTimeout(() => {
reject({
message: "Subscription verification timed out, this will need to be cleaned up",
subscriptionID: id,
});
delete this._subscriptionQueue[id];
}, 600000),
};
return this;
}
resolveSubscription(id) {
if (!this._subscriptionQueue[id]) {
return this;
}
const { resolve, timeout, data } = this._subscriptionQueue[id];
clearTimeout(timeout);
resolve(data);
delete this._subscriptionQueue[id];
return this;
}
}
const instance = new EventManager();
module.exports = instance;