@ozen-ui/data-grid
Version:
React DataGrid library
51 lines (50 loc) • 1.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventBus = void 0;
var EventBus = /** @class */ (function () {
function EventBus() {
this.listenersByEvent = {};
}
EventBus.prototype.subscribe = function (event, callback, isClearAfterCall) {
var _this = this;
if (isClearAfterCall === void 0) { isClearAfterCall = false; }
var listener = {
callback: callback,
isClearAfterCall: isClearAfterCall,
};
if (!this.listenersByEvent[event]) {
this.listenersByEvent[event] = new Set();
}
this.listenersByEvent[event].add(listener);
return function () {
_this.unsubscribe(event, listener);
};
};
EventBus.prototype.publish = function (event, payload) {
var _this = this;
var listeners = this.listenersByEvent[event];
if (!listeners) {
return;
}
listeners.forEach(function (listener) {
var callback = listener.callback, isClearAfterCall = listener.isClearAfterCall;
callback(payload);
if (isClearAfterCall) {
_this.unsubscribe(event, listener);
}
});
};
EventBus.prototype.clear = function (event) {
var _a;
(_a = this.listenersByEvent[event]) === null || _a === void 0 ? void 0 : _a.clear();
};
EventBus.prototype.unsubscribe = function (event, listener) {
var listeners = this.listenersByEvent[event];
if (!listeners) {
return;
}
listeners.delete(listener);
};
return EventBus;
}());
exports.EventBus = EventBus;