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