sub-events
Version:
Lightweight, strongly-typed events, with monitored subscriptions.
57 lines (56 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Subscription = void 0;
/**
* Represents an event subscription, and a safe way to cancel it.
*
* @see {@link cancel}
*/
var Subscription = /** @class */ (function () {
/**
* @hidden
*/
function Subscription(init) {
var _this = this;
this._cancel = init.cancel;
this.name = init.sub.name;
var cc = init.sub.cancel;
init.sub.cancel = function () {
_this._cancel = null;
cc();
};
}
Object.defineProperty(Subscription.prototype, "live", {
/**
* Indicates whether the subscription is live / active.
*
* It can be useful to subscribers when {@link SubEvent.cancelAll} is used without their knowledge.
*/
get: function () {
return !!this._cancel;
},
enumerable: false,
configurable: true
});
/**
* Cancels the live subscription. The subscriber won't receive any more events.
*
* It also sets flag {@link live} to `false`.
*
* @returns
* - `true` - subscription has been successfully cancelled
* - `false` - nothing happened, as subscription wasn't live
*
* @see {@link SubEvent.cancelAll}
*/
Subscription.prototype.cancel = function () {
if (this._cancel) {
this._cancel();
this._cancel = null;
return true;
}
return false;
};
return Subscription;
}());
exports.Subscription = Subscription;