2 var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 Object.defineProperty(exports, "__esModule", { value: true });
16 var isFunction_1 = require("./util/isFunction");
17 var Observer_1 = require("./Observer");
18 var Subscription_1 = require("./Subscription");
19 var rxSubscriber_1 = require("../internal/symbol/rxSubscriber");
20 var config_1 = require("./config");
21 var hostReportError_1 = require("./util/hostReportError");
22 var Subscriber = (function (_super) {
23 __extends(Subscriber, _super);
24 function Subscriber(destinationOrNext, error, complete) {
25 var _this = _super.call(this) || this;
26 _this.syncErrorValue = null;
27 _this.syncErrorThrown = false;
28 _this.syncErrorThrowable = false;
29 _this.isStopped = false;
30 switch (arguments.length) {
32 _this.destination = Observer_1.empty;
35 if (!destinationOrNext) {
36 _this.destination = Observer_1.empty;
39 if (typeof destinationOrNext === 'object') {
40 if (destinationOrNext instanceof Subscriber) {
41 _this.syncErrorThrowable = destinationOrNext.syncErrorThrowable;
42 _this.destination = destinationOrNext;
43 destinationOrNext.add(_this);
46 _this.syncErrorThrowable = true;
47 _this.destination = new SafeSubscriber(_this, destinationOrNext);
52 _this.syncErrorThrowable = true;
53 _this.destination = new SafeSubscriber(_this, destinationOrNext, error, complete);
58 Subscriber.prototype[rxSubscriber_1.rxSubscriber] = function () { return this; };
59 Subscriber.create = function (next, error, complete) {
60 var subscriber = new Subscriber(next, error, complete);
61 subscriber.syncErrorThrowable = false;
64 Subscriber.prototype.next = function (value) {
65 if (!this.isStopped) {
69 Subscriber.prototype.error = function (err) {
70 if (!this.isStopped) {
71 this.isStopped = true;
75 Subscriber.prototype.complete = function () {
76 if (!this.isStopped) {
77 this.isStopped = true;
81 Subscriber.prototype.unsubscribe = function () {
85 this.isStopped = true;
86 _super.prototype.unsubscribe.call(this);
88 Subscriber.prototype._next = function (value) {
89 this.destination.next(value);
91 Subscriber.prototype._error = function (err) {
92 this.destination.error(err);
95 Subscriber.prototype._complete = function () {
96 this.destination.complete();
99 Subscriber.prototype._unsubscribeAndRecycle = function () {
100 var _parentOrParents = this._parentOrParents;
101 this._parentOrParents = null;
104 this.isStopped = false;
105 this._parentOrParents = _parentOrParents;
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;
118 if (isFunction_1.isFunction(observerOrNext)) {
119 next = observerOrNext;
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));
130 context.unsubscribe = _this.unsubscribe.bind(_this);
133 _this._context = context;
135 _this._error = error;
136 _this._complete = complete;
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);
145 else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
150 SafeSubscriber.prototype.error = function (err) {
151 if (!this.isStopped) {
152 var _parentSubscriber = this._parentSubscriber;
153 var useDeprecatedSynchronousErrorHandling = config_1.config.useDeprecatedSynchronousErrorHandling;
155 if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
156 this.__tryOrUnsub(this._error, err);
160 this.__tryOrSetError(_parentSubscriber, this._error, err);
164 else if (!_parentSubscriber.syncErrorThrowable) {
166 if (useDeprecatedSynchronousErrorHandling) {
169 hostReportError_1.hostReportError(err);
172 if (useDeprecatedSynchronousErrorHandling) {
173 _parentSubscriber.syncErrorValue = err;
174 _parentSubscriber.syncErrorThrown = true;
177 hostReportError_1.hostReportError(err);
183 SafeSubscriber.prototype.complete = function () {
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);
194 this.__tryOrSetError(_parentSubscriber, wrappedComplete);
203 SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) {
205 fn.call(this._context, value);
209 if (config_1.config.useDeprecatedSynchronousErrorHandling) {
213 hostReportError_1.hostReportError(err);
217 SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) {
218 if (!config_1.config.useDeprecatedSynchronousErrorHandling) {
219 throw new Error('bad call');
222 fn.call(this._context, value);
225 if (config_1.config.useDeprecatedSynchronousErrorHandling) {
226 parent.syncErrorValue = err;
227 parent.syncErrorThrown = true;
231 hostReportError_1.hostReportError(err);
237 SafeSubscriber.prototype._unsubscribe = function () {
238 var _parentSubscriber = this._parentSubscriber;
239 this._context = null;
240 this._parentSubscriber = null;
241 _parentSubscriber.unsubscribe();
243 return SafeSubscriber;
245 exports.SafeSubscriber = SafeSubscriber;
246 //# sourceMappingURL=Subscriber.js.map