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