1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var isFunction_1 = require("./util/isFunction");
|
14 | var Observer_1 = require("./Observer");
|
15 | var Subscription_1 = require("./Subscription");
|
16 | var rxSubscriber_1 = require("../internal/symbol/rxSubscriber");
|
17 | var config_1 = require("./config");
|
18 | var hostReportError_1 = require("./util/hostReportError");
|
19 | var Subscriber = (function (_super) {
|
20 | __extends(Subscriber, _super);
|
21 | function Subscriber(destinationOrNext, error, complete) {
|
22 | var _this = _super.call(this) || this;
|
23 | _this.syncErrorValue = null;
|
24 | _this.syncErrorThrown = false;
|
25 | _this.syncErrorThrowable = false;
|
26 | _this.isStopped = false;
|
27 | switch (arguments.length) {
|
28 | case 0:
|
29 | _this.destination = Observer_1.empty;
|
30 | break;
|
31 | case 1:
|
32 | if (!destinationOrNext) {
|
33 | _this.destination = Observer_1.empty;
|
34 | break;
|
35 | }
|
36 | if (typeof destinationOrNext === 'object') {
|
37 | if (isTrustedSubscriber(destinationOrNext)) {
|
38 | var trustedSubscriber = destinationOrNext[rxSubscriber_1.rxSubscriber]();
|
39 | _this.syncErrorThrowable = trustedSubscriber.syncErrorThrowable;
|
40 | _this.destination = trustedSubscriber;
|
41 | trustedSubscriber.add(_this);
|
42 | }
|
43 | else {
|
44 | _this.syncErrorThrowable = true;
|
45 | _this.destination = new SafeSubscriber(_this, destinationOrNext);
|
46 | }
|
47 | break;
|
48 | }
|
49 | default:
|
50 | _this.syncErrorThrowable = true;
|
51 | _this.destination = new SafeSubscriber(_this, destinationOrNext, error, complete);
|
52 | break;
|
53 | }
|
54 | return _this;
|
55 | }
|
56 | Subscriber.prototype[rxSubscriber_1.rxSubscriber] = function () { return this; };
|
57 | Subscriber.create = function (next, error, complete) {
|
58 | var subscriber = new Subscriber(next, error, complete);
|
59 | subscriber.syncErrorThrowable = false;
|
60 | return subscriber;
|
61 | };
|
62 | Subscriber.prototype.next = function (value) {
|
63 | if (!this.isStopped) {
|
64 | this._next(value);
|
65 | }
|
66 | };
|
67 | Subscriber.prototype.error = function (err) {
|
68 | if (!this.isStopped) {
|
69 | this.isStopped = true;
|
70 | this._error(err);
|
71 | }
|
72 | };
|
73 | Subscriber.prototype.complete = function () {
|
74 | if (!this.isStopped) {
|
75 | this.isStopped = true;
|
76 | this._complete();
|
77 | }
|
78 | };
|
79 | Subscriber.prototype.unsubscribe = function () {
|
80 | if (this.closed) {
|
81 | return;
|
82 | }
|
83 | this.isStopped = true;
|
84 | _super.prototype.unsubscribe.call(this);
|
85 | };
|
86 | Subscriber.prototype._next = function (value) {
|
87 | this.destination.next(value);
|
88 | };
|
89 | Subscriber.prototype._error = function (err) {
|
90 | this.destination.error(err);
|
91 | this.unsubscribe();
|
92 | };
|
93 | Subscriber.prototype._complete = function () {
|
94 | this.destination.complete();
|
95 | this.unsubscribe();
|
96 | };
|
97 | Subscriber.prototype._unsubscribeAndRecycle = function () {
|
98 | var _a = this, _parent = _a._parent, _parents = _a._parents;
|
99 | this._parent = null;
|
100 | this._parents = null;
|
101 | this.unsubscribe();
|
102 | this.closed = false;
|
103 | this.isStopped = false;
|
104 | this._parent = _parent;
|
105 | this._parents = _parents;
|
106 | return this;
|
107 | };
|
108 | return Subscriber;
|
109 | }(Subscription_1.Subscription));
|
110 | exports.Subscriber = Subscriber;
|
111 | var SafeSubscriber = (function (_super) {
|
112 | __extends(SafeSubscriber, _super);
|
113 | function SafeSubscriber(_parentSubscriber, observerOrNext, error, complete) {
|
114 | var _this = _super.call(this) || this;
|
115 | _this._parentSubscriber = _parentSubscriber;
|
116 | var next;
|
117 | var context = _this;
|
118 | if (isFunction_1.isFunction(observerOrNext)) {
|
119 | next = observerOrNext;
|
120 | }
|
121 | else if (observerOrNext) {
|
122 | next = observerOrNext.next;
|
123 | error = observerOrNext.error;
|
124 | complete = observerOrNext.complete;
|
125 | if (observerOrNext !== Observer_1.empty) {
|
126 | context = Object.create(observerOrNext);
|
127 | if (isFunction_1.isFunction(context.unsubscribe)) {
|
128 | _this.add(context.unsubscribe.bind(context));
|
129 | }
|
130 | context.unsubscribe = _this.unsubscribe.bind(_this);
|
131 | }
|
132 | }
|
133 | _this._context = context;
|
134 | _this._next = next;
|
135 | _this._error = error;
|
136 | _this._complete = complete;
|
137 | return _this;
|
138 | }
|
139 | SafeSubscriber.prototype.next = function (value) {
|
140 | if (!this.isStopped && this._next) {
|
141 | var _parentSubscriber = this._parentSubscriber;
|
142 | if (!config_1.config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
|
143 | this.__tryOrUnsub(this._next, value);
|
144 | }
|
145 | else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
|
146 | this.unsubscribe();
|
147 | }
|
148 | }
|
149 | };
|
150 | SafeSubscriber.prototype.error = function (err) {
|
151 | if (!this.isStopped) {
|
152 | var _parentSubscriber = this._parentSubscriber;
|
153 | var useDeprecatedSynchronousErrorHandling = config_1.config.useDeprecatedSynchronousErrorHandling;
|
154 | if (this._error) {
|
155 | if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
|
156 | this.__tryOrUnsub(this._error, err);
|
157 | this.unsubscribe();
|
158 | }
|
159 | else {
|
160 | this.__tryOrSetError(_parentSubscriber, this._error, err);
|
161 | this.unsubscribe();
|
162 | }
|
163 | }
|
164 | else if (!_parentSubscriber.syncErrorThrowable) {
|
165 | this.unsubscribe();
|
166 | if (useDeprecatedSynchronousErrorHandling) {
|
167 | throw err;
|
168 | }
|
169 | hostReportError_1.hostReportError(err);
|
170 | }
|
171 | else {
|
172 | if (useDeprecatedSynchronousErrorHandling) {
|
173 | _parentSubscriber.syncErrorValue = err;
|
174 | _parentSubscriber.syncErrorThrown = true;
|
175 | }
|
176 | else {
|
177 | hostReportError_1.hostReportError(err);
|
178 | }
|
179 | this.unsubscribe();
|
180 | }
|
181 | }
|
182 | };
|
183 | SafeSubscriber.prototype.complete = function () {
|
184 | var _this = this;
|
185 | if (!this.isStopped) {
|
186 | var _parentSubscriber = this._parentSubscriber;
|
187 | if (this._complete) {
|
188 | var wrappedComplete = function () { return _this._complete.call(_this._context); };
|
189 | if (!config_1.config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
|
190 | this.__tryOrUnsub(wrappedComplete);
|
191 | this.unsubscribe();
|
192 | }
|
193 | else {
|
194 | this.__tryOrSetError(_parentSubscriber, wrappedComplete);
|
195 | this.unsubscribe();
|
196 | }
|
197 | }
|
198 | else {
|
199 | this.unsubscribe();
|
200 | }
|
201 | }
|
202 | };
|
203 | SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) {
|
204 | try {
|
205 | fn.call(this._context, value);
|
206 | }
|
207 | catch (err) {
|
208 | this.unsubscribe();
|
209 | if (config_1.config.useDeprecatedSynchronousErrorHandling) {
|
210 | throw err;
|
211 | }
|
212 | else {
|
213 | hostReportError_1.hostReportError(err);
|
214 | }
|
215 | }
|
216 | };
|
217 | SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) {
|
218 | if (!config_1.config.useDeprecatedSynchronousErrorHandling) {
|
219 | throw new Error('bad call');
|
220 | }
|
221 | try {
|
222 | fn.call(this._context, value);
|
223 | }
|
224 | catch (err) {
|
225 | if (config_1.config.useDeprecatedSynchronousErrorHandling) {
|
226 | parent.syncErrorValue = err;
|
227 | parent.syncErrorThrown = true;
|
228 | return true;
|
229 | }
|
230 | else {
|
231 | hostReportError_1.hostReportError(err);
|
232 | return true;
|
233 | }
|
234 | }
|
235 | return false;
|
236 | };
|
237 | SafeSubscriber.prototype._unsubscribe = function () {
|
238 | var _parentSubscriber = this._parentSubscriber;
|
239 | this._context = null;
|
240 | this._parentSubscriber = null;
|
241 | _parentSubscriber.unsubscribe();
|
242 | };
|
243 | return SafeSubscriber;
|
244 | }(Subscriber));
|
245 | function isTrustedSubscriber(obj) {
|
246 | return obj instanceof Subscriber || ('syncErrorThrowable' in obj && obj[rxSubscriber_1.rxSubscriber]);
|
247 | }
|
248 |
|
\ | No newline at end of file |