1 /** PURE_IMPORTS_START _util_isArray,_util_isObject,_util_isFunction,_util_UnsubscriptionError PURE_IMPORTS_END */
2 import { isArray } from './util/isArray';
3 import { isObject } from './util/isObject';
4 import { isFunction } from './util/isFunction';
5 import { UnsubscriptionError } from './util/UnsubscriptionError';
6 var Subscription = /*@__PURE__*/ (function () {
7 function Subscription(unsubscribe) {
9 this._parentOrParents = null;
10 this._subscriptions = null;
12 this._unsubscribe = unsubscribe;
15 Subscription.prototype.unsubscribe = function () {
20 var _a = this, _parentOrParents = _a._parentOrParents, _unsubscribe = _a._unsubscribe, _subscriptions = _a._subscriptions;
22 this._parentOrParents = null;
23 this._subscriptions = null;
24 if (_parentOrParents instanceof Subscription) {
25 _parentOrParents.remove(this);
27 else if (_parentOrParents !== null) {
28 for (var index = 0; index < _parentOrParents.length; ++index) {
29 var parent_1 = _parentOrParents[index];
30 parent_1.remove(this);
33 if (isFunction(_unsubscribe)) {
35 _unsubscribe.call(this);
38 errors = e instanceof UnsubscriptionError ? flattenUnsubscriptionErrors(e.errors) : [e];
41 if (isArray(_subscriptions)) {
43 var len = _subscriptions.length;
44 while (++index < len) {
45 var sub = _subscriptions[index];
51 errors = errors || [];
52 if (e instanceof UnsubscriptionError) {
53 errors = errors.concat(flattenUnsubscriptionErrors(e.errors));
63 throw new UnsubscriptionError(errors);
66 Subscription.prototype.add = function (teardown) {
67 var subscription = teardown;
69 return Subscription.EMPTY;
71 switch (typeof teardown) {
73 subscription = new Subscription(teardown);
75 if (subscription === this || subscription.closed || typeof subscription.unsubscribe !== 'function') {
78 else if (this.closed) {
79 subscription.unsubscribe();
82 else if (!(subscription instanceof Subscription)) {
83 var tmp = subscription;
84 subscription = new Subscription();
85 subscription._subscriptions = [tmp];
89 throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.');
92 var _parentOrParents = subscription._parentOrParents;
93 if (_parentOrParents === null) {
94 subscription._parentOrParents = this;
96 else if (_parentOrParents instanceof Subscription) {
97 if (_parentOrParents === this) {
100 subscription._parentOrParents = [_parentOrParents, this];
102 else if (_parentOrParents.indexOf(this) === -1) {
103 _parentOrParents.push(this);
108 var subscriptions = this._subscriptions;
109 if (subscriptions === null) {
110 this._subscriptions = [subscription];
113 subscriptions.push(subscription);
117 Subscription.prototype.remove = function (subscription) {
118 var subscriptions = this._subscriptions;
120 var subscriptionIndex = subscriptions.indexOf(subscription);
121 if (subscriptionIndex !== -1) {
122 subscriptions.splice(subscriptionIndex, 1);
126 Subscription.EMPTY = (function (empty) {
129 }(new Subscription()));
132 export { Subscription };
133 function flattenUnsubscriptionErrors(errors) {
134 return errors.reduce(function (errs, err) { return errs.concat((err instanceof UnsubscriptionError) ? err.errors : err); }, []);
136 //# sourceMappingURL=Subscription.js.map