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) {
|
---|
8 | this.closed = false;
|
---|
9 | this._parentOrParents = null;
|
---|
10 | this._subscriptions = null;
|
---|
11 | if (unsubscribe) {
|
---|
12 | this._ctorUnsubscribe = true;
|
---|
13 | this._unsubscribe = unsubscribe;
|
---|
14 | }
|
---|
15 | }
|
---|
16 | Subscription.prototype.unsubscribe = function () {
|
---|
17 | var errors;
|
---|
18 | if (this.closed) {
|
---|
19 | return;
|
---|
20 | }
|
---|
21 | var _a = this, _parentOrParents = _a._parentOrParents, _ctorUnsubscribe = _a._ctorUnsubscribe, _unsubscribe = _a._unsubscribe, _subscriptions = _a._subscriptions;
|
---|
22 | this.closed = true;
|
---|
23 | this._parentOrParents = null;
|
---|
24 | this._subscriptions = null;
|
---|
25 | if (_parentOrParents instanceof Subscription) {
|
---|
26 | _parentOrParents.remove(this);
|
---|
27 | }
|
---|
28 | else if (_parentOrParents !== null) {
|
---|
29 | for (var index = 0; index < _parentOrParents.length; ++index) {
|
---|
30 | var parent_1 = _parentOrParents[index];
|
---|
31 | parent_1.remove(this);
|
---|
32 | }
|
---|
33 | }
|
---|
34 | if (isFunction(_unsubscribe)) {
|
---|
35 | if (_ctorUnsubscribe) {
|
---|
36 | this._unsubscribe = undefined;
|
---|
37 | }
|
---|
38 | try {
|
---|
39 | _unsubscribe.call(this);
|
---|
40 | }
|
---|
41 | catch (e) {
|
---|
42 | errors = e instanceof UnsubscriptionError ? flattenUnsubscriptionErrors(e.errors) : [e];
|
---|
43 | }
|
---|
44 | }
|
---|
45 | if (isArray(_subscriptions)) {
|
---|
46 | var index = -1;
|
---|
47 | var len = _subscriptions.length;
|
---|
48 | while (++index < len) {
|
---|
49 | var sub = _subscriptions[index];
|
---|
50 | if (isObject(sub)) {
|
---|
51 | try {
|
---|
52 | sub.unsubscribe();
|
---|
53 | }
|
---|
54 | catch (e) {
|
---|
55 | errors = errors || [];
|
---|
56 | if (e instanceof UnsubscriptionError) {
|
---|
57 | errors = errors.concat(flattenUnsubscriptionErrors(e.errors));
|
---|
58 | }
|
---|
59 | else {
|
---|
60 | errors.push(e);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | if (errors) {
|
---|
67 | throw new UnsubscriptionError(errors);
|
---|
68 | }
|
---|
69 | };
|
---|
70 | Subscription.prototype.add = function (teardown) {
|
---|
71 | var subscription = teardown;
|
---|
72 | if (!teardown) {
|
---|
73 | return Subscription.EMPTY;
|
---|
74 | }
|
---|
75 | switch (typeof teardown) {
|
---|
76 | case 'function':
|
---|
77 | subscription = new Subscription(teardown);
|
---|
78 | case 'object':
|
---|
79 | if (subscription === this || subscription.closed || typeof subscription.unsubscribe !== 'function') {
|
---|
80 | return subscription;
|
---|
81 | }
|
---|
82 | else if (this.closed) {
|
---|
83 | subscription.unsubscribe();
|
---|
84 | return subscription;
|
---|
85 | }
|
---|
86 | else if (!(subscription instanceof Subscription)) {
|
---|
87 | var tmp = subscription;
|
---|
88 | subscription = new Subscription();
|
---|
89 | subscription._subscriptions = [tmp];
|
---|
90 | }
|
---|
91 | break;
|
---|
92 | default: {
|
---|
93 | throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.');
|
---|
94 | }
|
---|
95 | }
|
---|
96 | var _parentOrParents = subscription._parentOrParents;
|
---|
97 | if (_parentOrParents === null) {
|
---|
98 | subscription._parentOrParents = this;
|
---|
99 | }
|
---|
100 | else if (_parentOrParents instanceof Subscription) {
|
---|
101 | if (_parentOrParents === this) {
|
---|
102 | return subscription;
|
---|
103 | }
|
---|
104 | subscription._parentOrParents = [_parentOrParents, this];
|
---|
105 | }
|
---|
106 | else if (_parentOrParents.indexOf(this) === -1) {
|
---|
107 | _parentOrParents.push(this);
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | return subscription;
|
---|
111 | }
|
---|
112 | var subscriptions = this._subscriptions;
|
---|
113 | if (subscriptions === null) {
|
---|
114 | this._subscriptions = [subscription];
|
---|
115 | }
|
---|
116 | else {
|
---|
117 | subscriptions.push(subscription);
|
---|
118 | }
|
---|
119 | return subscription;
|
---|
120 | };
|
---|
121 | Subscription.prototype.remove = function (subscription) {
|
---|
122 | var subscriptions = this._subscriptions;
|
---|
123 | if (subscriptions) {
|
---|
124 | var subscriptionIndex = subscriptions.indexOf(subscription);
|
---|
125 | if (subscriptionIndex !== -1) {
|
---|
126 | subscriptions.splice(subscriptionIndex, 1);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | };
|
---|
130 | Subscription.EMPTY = (function (empty) {
|
---|
131 | empty.closed = true;
|
---|
132 | return empty;
|
---|
133 | }(new Subscription()));
|
---|
134 | return Subscription;
|
---|
135 | }());
|
---|
136 | export { Subscription };
|
---|
137 | function flattenUnsubscriptionErrors(errors) {
|
---|
138 | return errors.reduce(function (errs, err) { return errs.concat((err instanceof UnsubscriptionError) ? err.errors : err); }, []);
|
---|
139 | }
|
---|
140 | //# sourceMappingURL=Subscription.js.map
|
---|