UNPKG

@acutmore/rxjs

Version:

Reactive Extensions for modern JavaScript

248 lines (220 loc) 7.18 kB
import { isFunction } from './util/isFunction'; import { Observer, PartialObserver, empty as emptyObserver } from './Observer'; import { Subscription } from './Subscription'; import { rxSubscriber as rxSubscriberSymbol } from '../internal/symbol/rxSubscriber'; /** * Implements the {@link Observer} interface and extends the * {@link Subscription} class. While the {@link Observer} is the public API for * consuming the values of an {@link Observable}, all Observers get converted to * a Subscriber, in order to provide Subscription-like capabilities such as * `unsubscribe`. Subscriber is a common type in RxJS, and crucial for * implementing operators, but it is rarely used as a public API. * * @class Subscriber<T> */ export class Subscriber<T> extends Subscription implements Observer<T> { [rxSubscriberSymbol]() { return this; } /** * A static factory for a Subscriber, given a (potentially partial) definition * of an Observer. * @param {function(x: ?T): void} [next] The `next` callback of an Observer. * @param {function(e: ?any): void} [error] The `error` callback of an * Observer. * @param {function(): void} [complete] The `complete` callback of an * Observer. * @return {Subscriber<T>} A Subscriber wrapping the (partially defined) * Observer represented by the given arguments. */ static create<T>(next?: (x?: T) => void, error?: (e?: any) => void, complete?: () => void): Subscriber<T> { const subscriber = new Subscriber(next, error, complete); return subscriber; } protected isStopped: boolean = false; protected destination: PartialObserver<any>; // this `any` is the escape hatch to erase extra type param (e.g. R) /** * @param {Observer|function(value: T): void} [destinationOrNext] A partially * defined Observer or a `next` callback function. * @param {function(e: ?any): void} [error] The `error` callback of an * Observer. * @param {function(): void} [complete] The `complete` callback of an * Observer. */ constructor(destinationOrNext?: PartialObserver<any> | ((value: T) => void), error?: (e?: any) => void, complete?: () => void) { super(); 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.destination = (<Subscriber<any>> destinationOrNext); (<any> this.destination).add(this); } else { this.destination = new SafeSubscriber<T>(<PartialObserver<any>> destinationOrNext); } break; } default: this.destination = new SafeSubscriber<T>(<((value: T) => void)> destinationOrNext, error, complete); break; } } /** * The {@link Observer} callback to receive notifications of type `next` from * the Observable, with a value. The Observable may call this method 0 or more * times. * @param {T} [value] The `next` value. * @return {void} */ next(value?: T): void { if (!this.isStopped) { this._next(value); } } /** * The {@link Observer} callback to receive notifications of type `error` from * the Observable, with an attached {@link Error}. Notifies the Observer that * the Observable has experienced an error condition. * @param {any} [err] The `error` exception. * @return {void} */ error(err?: any): void { if (!this.isStopped) { this.isStopped = true; this._error(err); } } /** * The {@link Observer} callback to receive a valueless notification of type * `complete` from the Observable. Notifies the Observer that the Observable * has finished sending push-based notifications. * @return {void} */ complete(): void { if (!this.isStopped) { this.isStopped = true; this._complete(); } } unsubscribe(): void { if (this.closed) { return; } this.isStopped = true; super.unsubscribe(); } protected _next(value: T): void { this.destination.next(value); } protected _error(err: any): void { this.destination.error(err); this.unsubscribe(); } protected _complete(): void { this.destination.complete(); this.unsubscribe(); } protected _unsubscribeAndRecycle(): Subscriber<T> { const { _parent, _parents } = this; this._parent = null; this._parents = null; this.unsubscribe(); this.closed = false; this.isStopped = false; this._parent = _parent; this._parents = _parents; return this; } // v4-backwards-compatibility onNext: this['next']; onError: this['error']; onCompleted: this['complete']; } // v4-backwards-compatibility Subscriber.prototype.onNext = function(v) { this.next(v); }; Subscriber.prototype.onError = function(e) { this.error(e); }; Subscriber.prototype.onCompleted = function() { this.complete(); }; /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class SafeSubscriber<T> extends Subscriber<T> { private _context: any; constructor(observerOrNext?: PartialObserver<T> | ((value: T) => void), error?: (e?: any) => void, complete?: () => void) { super(); let next: ((value: T) => void); let context: any = this; if (isFunction(observerOrNext)) { next = (<((value: T) => void)> observerOrNext); } else if (observerOrNext) { next = (<PartialObserver<T>> observerOrNext).next; error = (<PartialObserver<T>> observerOrNext).error; complete = (<PartialObserver<T>> observerOrNext).complete; if (observerOrNext !== emptyObserver) { context = Object.create(observerOrNext); if (isFunction(context.unsubscribe)) { this.add(<() => void> context.unsubscribe.bind(context)); } context.unsubscribe = this.unsubscribe.bind(this); } } this._context = context; this._next = next; this._error = error; this._complete = complete; } next(value?: T): void { if (!this.isStopped && this._next) { try { this._next.call(this._context, value); } catch (err) { this._hostReportError(err); this.unsubscribe(); } } } error(err?: any): void { if (!this.isStopped) { if (this._error) { try { this._error.call(this._context, err); } catch (err) { this._hostReportError(err); } } else { this._hostReportError(err); } this.unsubscribe(); } } complete(): void { if (!this.isStopped) { if (this._complete) { try { this._complete.call(this._context); } catch (err) { this._hostReportError(err); } } this.unsubscribe(); } } protected _unsubscribe(): void { this._context = null; } private _hostReportError(err: any) { setTimeout(() => { throw err; }); } }