UNPKG

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