@easyquery/core
Version:
EasyQuery.JS core modules
101 lines • 3.35 kB
JavaScript
var EqGuid = /** @class */ (function () {
function EqGuid() {
}
EqGuid.newGuid = function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
return EqGuid;
}());
export { EqGuid };
var EventEmitter = /** @class */ (function () {
function EventEmitter(source) {
this.silentMode = 0;
this.events = new Array();
this.source = source;
}
EventEmitter.prototype.subscribe = function (eventType, callback) {
var event = this.getEventCallbacksByType(eventType);
var eventCallback = {
id: EqGuid.newGuid(),
callback: callback
};
if (event) {
event.eventCallbacks.push(eventCallback);
}
else {
event = {
type: eventType,
eventCallbacks: new Array(eventCallback)
};
this.events.push(event);
}
return eventCallback.id;
};
EventEmitter.prototype.unsubscribe = function (eventType, callbackId) {
var event = this.getEventCallbacksByType(eventType);
if (event) {
var index = -1;
for (index = 0; index < event.eventCallbacks.length; index++) {
if (event.eventCallbacks[index].id === callbackId) {
break;
}
}
if (index >= 0) {
event.eventCallbacks.splice(index, 1);
}
}
};
EventEmitter.prototype.fire = function (eventType, data, postpone, force) {
if (postpone === void 0) { postpone = 0; }
if (force === void 0) { force = false; }
if (this.silentMode && !force) {
return;
}
var event = this.getEventCallbacksByType(eventType);
if (event) {
var eqevent_1 = {
type: eventType,
source: this.source,
data: data
};
var emitAllFunc = function () {
for (var _i = 0, _a = event.eventCallbacks; _i < _a.length; _i++) {
var callback = _a[_i];
callback.callback(eqevent_1);
}
};
if (postpone > 0) {
setTimeout(emitAllFunc, postpone);
}
else {
emitAllFunc();
}
}
};
EventEmitter.prototype.enterSilentMode = function () {
this.silentMode++;
};
EventEmitter.prototype.exitSilentMode = function () {
if (this.silentMode) {
this.silentMode--;
}
};
EventEmitter.prototype.isSilent = function () {
return this.silentMode > 0;
};
EventEmitter.prototype.getEventCallbacksByType = function (eventType) {
for (var _i = 0, _a = this.events; _i < _a.length; _i++) {
var event_1 = _a[_i];
if (event_1.type == eventType) {
return event_1;
}
}
return null;
};
return EventEmitter;
}());
export { EventEmitter };
//# sourceMappingURL=event_emitter.js.map