replay-viewer
Version:
Rocket League replay viewer React component and tooling
57 lines • 2.08 kB
JavaScript
var EventBus = /** @class */ (function () {
function EventBus() {
this.listeners = {};
}
EventBus.prototype.buildEvent = function (type, scope) {
var _this = this;
var addEventListener = function (callback) {
return _this.addEventListener(type, callback, scope);
};
var removeEventListener = function (callback) {
return _this.removeEventListener(type, callback, scope);
};
var dispatch = function (args) { return _this.dispatch(type, args); };
return {
addEventListener: addEventListener,
removeEventListener: removeEventListener,
dispatch: dispatch,
};
};
EventBus.prototype.addEventListener = function (type, callback, scope) {
if (!this.listeners[type]) {
this.listeners[type] = [];
}
// Remove duplicate event listeners
this.removeEventListener(type, callback, scope);
// Add this listener to the list
this.listeners[type].push({ type: type, callback: callback, scope: scope });
};
EventBus.prototype.removeEventListener = function (type, callback, scope) {
if (!this.listeners[type]) {
return;
}
this.listeners[type] = this.listeners[type].filter(function (_a) {
var t = _a.type, c = _a.callback, s = _a.scope;
return !(type === t && callback === c && scope === s);
});
};
EventBus.prototype.dispatch = function (type, arg) {
if (!this.listeners[type]) {
return;
}
this.listeners[type].forEach(function (listener) {
if (type === listener.type) {
listener.callback.call(listener.scope || null, arg);
}
});
};
EventBus.prototype.reset = function () {
var _this = this;
Object.keys(this.listeners).forEach(function (key) {
delete _this.listeners[key];
});
};
return EventBus;
}());
export default new EventBus();
//# sourceMappingURL=EventBus.js.map