ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
63 lines (62 loc) • 1.97 kB
JavaScript
;
var __values = (this && this.__values)/* istanbul ignore next */ || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Event container for event subscriptions.
*/
var EventContainer = /** @class */ (function () {
function EventContainer() {
this.subscriptions = [];
}
/**
* Subscribe to an event being fired.
* @param subscription - Subscription.
*/
EventContainer.prototype.subscribe = function (subscription) {
var index = this.getIndex(subscription);
if (index === -1)
this.subscriptions.push(subscription);
};
/**
* Unsubscribe to an event being fired.
* @param subscription - Subscription.
*/
EventContainer.prototype.unsubscribe = function (subscription) {
var index = this.getIndex(subscription);
if (index >= 0)
this.subscriptions.splice(index, 1);
};
/**
* Fire an event.
*/
EventContainer.prototype.fire = function (arg) {
try {
for (var _a = __values(this.subscriptions), _b = _a.next(); !_b.done; _b = _a.next()) {
var subscription = _b.value;
subscription(arg);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
var e_1, _c;
};
EventContainer.prototype.getIndex = function (subscription) {
return this.subscriptions.indexOf(subscription);
};
return EventContainer;
}());
exports.EventContainer = EventContainer;