UNPKG

sub-events

Version:

Lightweight, strongly-typed events, with monitored subscriptions.

65 lines (64 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubEventCount = void 0; var tslib_1 = require("tslib"); var event_1 = require("./event"); /** * Extends {@link SubEvent} with event {@link onCount}, to observe the number of subscriptions. */ var SubEventCount = /** @class */ (function (_super) { tslib_1.__extends(SubEventCount, _super); /** * Event constructor. * * @param options * Configuration Options. */ function SubEventCount(options) { var _this = _super.call(this, options) || this; /** * Triggered on any change in the number of subscriptions. * @event onCount */ _this.onCount = new event_1.SubEvent(); var eo = options && options.emitOptions; _this._notify = function (data) { return _this.onCount.emit(data, eo); }; return _this; } /** * Cancels all existing subscriptions for the event. * * It overrides the base implementation, to trigger event {@link onCount} * when there was at least one subscription. * * @returns * Number of subscriptions cancelled. * * @see {@link Subscription.cancel} */ SubEventCount.prototype.cancelAll = function () { var prevCount = this.count; if (prevCount) { _super.prototype.cancelAll.call(this); this._notify({ newCount: 0, prevCount: prevCount }); } return prevCount; }; /** * Overrides base implementation, to trigger event {@link onCount} during * `subscribe` and `cancel` calls. * * @hidden */ SubEventCount.prototype._createCancel = function (sub) { var _this = this; var s = this._subs; this._notify({ newCount: s.length, prevCount: s.length - 1 }); return function () { _this._cancelSub(sub); _this._notify({ newCount: s.length, prevCount: s.length + 1 }); }; }; return SubEventCount; }(event_1.SubEvent)); exports.SubEventCount = SubEventCount;