You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
6.9 KiB
178 lines
6.9 KiB
"use strict";
|
|
var __values = (this && this.__values) || function(o) {
|
|
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
if (m) return m.call(o);
|
|
if (o && typeof o.length === "number") return {
|
|
next: function () {
|
|
if (o && i >= o.length) o = void 0;
|
|
return { value: o && o[i++], done: !o };
|
|
}
|
|
};
|
|
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
};
|
|
var __read = (this && this.__read) || function (o, n) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
if (!m) return o;
|
|
var i = m.call(o), r, ar = [], e;
|
|
try {
|
|
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
}
|
|
catch (error) { e = { error: error }; }
|
|
finally {
|
|
try {
|
|
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
}
|
|
finally { if (e) throw e.error; }
|
|
}
|
|
return ar;
|
|
};
|
|
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
to[j] = from[i];
|
|
return to;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.isSubscription = exports.EMPTY_SUBSCRIPTION = exports.Subscription = void 0;
|
|
var isFunction_1 = require("./util/isFunction");
|
|
var UnsubscriptionError_1 = require("./util/UnsubscriptionError");
|
|
var arrRemove_1 = require("./util/arrRemove");
|
|
var Subscription = (function () {
|
|
function Subscription(initialTeardown) {
|
|
this.initialTeardown = initialTeardown;
|
|
this.closed = false;
|
|
this._parentage = null;
|
|
this._teardowns = null;
|
|
}
|
|
Subscription.prototype.unsubscribe = function () {
|
|
var e_1, _a, e_2, _b;
|
|
var errors;
|
|
if (!this.closed) {
|
|
this.closed = true;
|
|
var _parentage = this._parentage;
|
|
if (_parentage) {
|
|
this._parentage = null;
|
|
if (Array.isArray(_parentage)) {
|
|
try {
|
|
for (var _parentage_1 = __values(_parentage), _parentage_1_1 = _parentage_1.next(); !_parentage_1_1.done; _parentage_1_1 = _parentage_1.next()) {
|
|
var parent_1 = _parentage_1_1.value;
|
|
parent_1.remove(this);
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (_parentage_1_1 && !_parentage_1_1.done && (_a = _parentage_1.return)) _a.call(_parentage_1);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
}
|
|
else {
|
|
_parentage.remove(this);
|
|
}
|
|
}
|
|
var initialTeardown = this.initialTeardown;
|
|
if (isFunction_1.isFunction(initialTeardown)) {
|
|
try {
|
|
initialTeardown();
|
|
}
|
|
catch (e) {
|
|
errors = e instanceof UnsubscriptionError_1.UnsubscriptionError ? e.errors : [e];
|
|
}
|
|
}
|
|
var _teardowns = this._teardowns;
|
|
if (_teardowns) {
|
|
this._teardowns = null;
|
|
try {
|
|
for (var _teardowns_1 = __values(_teardowns), _teardowns_1_1 = _teardowns_1.next(); !_teardowns_1_1.done; _teardowns_1_1 = _teardowns_1.next()) {
|
|
var teardown_1 = _teardowns_1_1.value;
|
|
try {
|
|
execTeardown(teardown_1);
|
|
}
|
|
catch (err) {
|
|
errors = errors !== null && errors !== void 0 ? errors : [];
|
|
if (err instanceof UnsubscriptionError_1.UnsubscriptionError) {
|
|
errors = __spreadArray(__spreadArray([], __read(errors)), __read(err.errors));
|
|
}
|
|
else {
|
|
errors.push(err);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
finally {
|
|
try {
|
|
if (_teardowns_1_1 && !_teardowns_1_1.done && (_b = _teardowns_1.return)) _b.call(_teardowns_1);
|
|
}
|
|
finally { if (e_2) throw e_2.error; }
|
|
}
|
|
}
|
|
if (errors) {
|
|
throw new UnsubscriptionError_1.UnsubscriptionError(errors);
|
|
}
|
|
}
|
|
};
|
|
Subscription.prototype.add = function (teardown) {
|
|
var _a;
|
|
if (teardown && teardown !== this) {
|
|
if (this.closed) {
|
|
execTeardown(teardown);
|
|
}
|
|
else {
|
|
if (teardown instanceof Subscription) {
|
|
if (teardown.closed || teardown._hasParent(this)) {
|
|
return;
|
|
}
|
|
teardown._addParent(this);
|
|
}
|
|
(this._teardowns = (_a = this._teardowns) !== null && _a !== void 0 ? _a : []).push(teardown);
|
|
}
|
|
}
|
|
};
|
|
Subscription.prototype._hasParent = function (parent) {
|
|
var _parentage = this._parentage;
|
|
return _parentage === parent || (Array.isArray(_parentage) && _parentage.includes(parent));
|
|
};
|
|
Subscription.prototype._addParent = function (parent) {
|
|
var _parentage = this._parentage;
|
|
this._parentage = Array.isArray(_parentage) ? (_parentage.push(parent), _parentage) : _parentage ? [_parentage, parent] : parent;
|
|
};
|
|
Subscription.prototype._removeParent = function (parent) {
|
|
var _parentage = this._parentage;
|
|
if (_parentage === parent) {
|
|
this._parentage = null;
|
|
}
|
|
else if (Array.isArray(_parentage)) {
|
|
arrRemove_1.arrRemove(_parentage, parent);
|
|
}
|
|
};
|
|
Subscription.prototype.remove = function (teardown) {
|
|
var _teardowns = this._teardowns;
|
|
_teardowns && arrRemove_1.arrRemove(_teardowns, teardown);
|
|
if (teardown instanceof Subscription) {
|
|
teardown._removeParent(this);
|
|
}
|
|
};
|
|
Subscription.EMPTY = (function () {
|
|
var empty = new Subscription();
|
|
empty.closed = true;
|
|
return empty;
|
|
})();
|
|
return Subscription;
|
|
}());
|
|
exports.Subscription = Subscription;
|
|
exports.EMPTY_SUBSCRIPTION = Subscription.EMPTY;
|
|
function isSubscription(value) {
|
|
return (value instanceof Subscription ||
|
|
(value && 'closed' in value && isFunction_1.isFunction(value.remove) && isFunction_1.isFunction(value.add) && isFunction_1.isFunction(value.unsubscribe)));
|
|
}
|
|
exports.isSubscription = isSubscription;
|
|
function execTeardown(teardown) {
|
|
if (isFunction_1.isFunction(teardown)) {
|
|
teardown();
|
|
}
|
|
else {
|
|
teardown.unsubscribe();
|
|
}
|
|
}
|
|
//# sourceMappingURL=Subscription.js.map
|