|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- import { isFunction } from './util/isFunction';
- import { empty as emptyObserver } from './Observer';
- import { Subscription } from './Subscription';
- import { rxSubscriber as rxSubscriberSymbol } from '../internal/symbol/rxSubscriber';
- import { config } from './config';
- import { hostReportError } from './util/hostReportError';
- export class Subscriber extends Subscription {
- constructor(destinationOrNext, error, complete) {
- super();
- this.syncErrorValue = null;
- this.syncErrorThrown = false;
- this.syncErrorThrowable = false;
- this.isStopped = false;
- this._parentSubscription = null;
- switch (arguments.length) {
- case 0:
- this.destination = emptyObserver;
- break;
- case 1:
- if (!destinationOrNext) {
- this.destination = emptyObserver;
- break;
- }
- if (typeof destinationOrNext === 'object') {
- if (destinationOrNext instanceof Subscriber) {
- this.syncErrorThrowable = destinationOrNext.syncErrorThrowable;
- this.destination = destinationOrNext;
- destinationOrNext.add(this);
- }
- else {
- this.syncErrorThrowable = true;
- this.destination = new SafeSubscriber(this, destinationOrNext);
- }
- break;
- }
- default:
- this.syncErrorThrowable = true;
- this.destination = new SafeSubscriber(this, destinationOrNext, error, complete);
- break;
- }
- }
- [rxSubscriberSymbol]() { return this; }
- static create(next, error, complete) {
- const subscriber = new Subscriber(next, error, complete);
- subscriber.syncErrorThrowable = false;
- return subscriber;
- }
- next(value) {
- if (!this.isStopped) {
- this._next(value);
- }
- }
- error(err) {
- if (!this.isStopped) {
- this.isStopped = true;
- this._error(err);
- }
- }
- complete() {
- if (!this.isStopped) {
- this.isStopped = true;
- this._complete();
- }
- }
- unsubscribe() {
- if (this.closed) {
- return;
- }
- this.isStopped = true;
- super.unsubscribe();
- }
- _next(value) {
- this.destination.next(value);
- }
- _error(err) {
- this.destination.error(err);
- this.unsubscribe();
- }
- _complete() {
- this.destination.complete();
- this.unsubscribe();
- }
- _unsubscribeAndRecycle() {
- const { _parent, _parents } = this;
- this._parent = null;
- this._parents = null;
- this.unsubscribe();
- this.closed = false;
- this.isStopped = false;
- this._parent = _parent;
- this._parents = _parents;
- this._parentSubscription = null;
- return this;
- }
- }
- export class SafeSubscriber extends Subscriber {
- constructor(_parentSubscriber, observerOrNext, error, complete) {
- super();
- this._parentSubscriber = _parentSubscriber;
- let next;
- let context = this;
- if (isFunction(observerOrNext)) {
- next = observerOrNext;
- }
- else if (observerOrNext) {
- next = observerOrNext.next;
- error = observerOrNext.error;
- complete = observerOrNext.complete;
- if (observerOrNext !== emptyObserver) {
- context = Object.create(observerOrNext);
- if (isFunction(context.unsubscribe)) {
- this.add(context.unsubscribe.bind(context));
- }
- context.unsubscribe = this.unsubscribe.bind(this);
- }
- }
- this._context = context;
- this._next = next;
- this._error = error;
- this._complete = complete;
- }
- next(value) {
- if (!this.isStopped && this._next) {
- const { _parentSubscriber } = this;
- if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
- this.__tryOrUnsub(this._next, value);
- }
- else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
- this.unsubscribe();
- }
- }
- }
- error(err) {
- if (!this.isStopped) {
- const { _parentSubscriber } = this;
- const { useDeprecatedSynchronousErrorHandling } = config;
- if (this._error) {
- if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
- this.__tryOrUnsub(this._error, err);
- this.unsubscribe();
- }
- else {
- this.__tryOrSetError(_parentSubscriber, this._error, err);
- this.unsubscribe();
- }
- }
- else if (!_parentSubscriber.syncErrorThrowable) {
- this.unsubscribe();
- if (useDeprecatedSynchronousErrorHandling) {
- throw err;
- }
- hostReportError(err);
- }
- else {
- if (useDeprecatedSynchronousErrorHandling) {
- _parentSubscriber.syncErrorValue = err;
- _parentSubscriber.syncErrorThrown = true;
- }
- else {
- hostReportError(err);
- }
- this.unsubscribe();
- }
- }
- }
- complete() {
- if (!this.isStopped) {
- const { _parentSubscriber } = this;
- if (this._complete) {
- const wrappedComplete = () => this._complete.call(this._context);
- if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
- this.__tryOrUnsub(wrappedComplete);
- this.unsubscribe();
- }
- else {
- this.__tryOrSetError(_parentSubscriber, wrappedComplete);
- this.unsubscribe();
- }
- }
- else {
- this.unsubscribe();
- }
- }
- }
- __tryOrUnsub(fn, value) {
- try {
- fn.call(this._context, value);
- }
- catch (err) {
- this.unsubscribe();
- if (config.useDeprecatedSynchronousErrorHandling) {
- throw err;
- }
- else {
- hostReportError(err);
- }
- }
- }
- __tryOrSetError(parent, fn, value) {
- if (!config.useDeprecatedSynchronousErrorHandling) {
- throw new Error('bad call');
- }
- try {
- fn.call(this._context, value);
- }
- catch (err) {
- if (config.useDeprecatedSynchronousErrorHandling) {
- parent.syncErrorValue = err;
- parent.syncErrorThrown = true;
- return true;
- }
- else {
- hostReportError(err);
- return true;
- }
- }
- return false;
- }
- _unsubscribe() {
- const { _parentSubscriber } = this;
- this._context = null;
- this._parentSubscriber = null;
- _parentSubscriber.unsubscribe();
- }
- }
- //# sourceMappingURL=Subscriber.js.map
|