es6/Subscriber.js
- import { isFunction } from './util/isFunction';
- import { Subscription } from './Subscription';
- import { empty as emptyObserver } from './Observer';
- import { $$rxSubscriber } from './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 var Subscriber = (function (_super) {
- __extends(Subscriber, _super);
- /**
- * @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.
- */
- function Subscriber(destinationOrNext, error, complete) {
- _super.call(this);
- this.syncErrorValue = null;
- this.syncErrorThrown = false;
- this.syncErrorThrowable = false;
- this.isStopped = false;
- 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 = destinationOrNext;
- this.destination.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;
- }
- }
- Subscriber.prototype[$$rxSubscriber] = function () { 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.
- */
- Subscriber.create = function (next, error, complete) {
- var subscriber = new Subscriber(next, error, complete);
- subscriber.syncErrorThrowable = false;
- return subscriber;
- };
- /**
- * 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}
- */
- Subscriber.prototype.next = function (value) {
- 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}
- */
- Subscriber.prototype.error = function (err) {
- 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}
- */
- Subscriber.prototype.complete = function () {
- if (!this.isStopped) {
- this.isStopped = true;
- this._complete();
- }
- };
- Subscriber.prototype.unsubscribe = function () {
- if (this.closed) {
- return;
- }
- this.isStopped = true;
- _super.prototype.unsubscribe.call(this);
- };
- Subscriber.prototype._next = function (value) {
- this.destination.next(value);
- };
- Subscriber.prototype._error = function (err) {
- this.destination.error(err);
- this.unsubscribe();
- };
- Subscriber.prototype._complete = function () {
- this.destination.complete();
- this.unsubscribe();
- };
- return Subscriber;
- }(Subscription));
- /**
- * We need this JSDoc comment for affecting ESDoc.
- * @ignore
- * @extends {Ignored}
- */
- var SafeSubscriber = (function (_super) {
- __extends(SafeSubscriber, _super);
- function SafeSubscriber(_parent, observerOrNext, error, complete) {
- _super.call(this);
- this._parent = _parent;
- var next;
- var context = this;
- if (isFunction(observerOrNext)) {
- next = observerOrNext;
- }
- else if (observerOrNext) {
- context = observerOrNext;
- next = observerOrNext.next;
- error = observerOrNext.error;
- complete = observerOrNext.complete;
- 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;
- }
- SafeSubscriber.prototype.next = function (value) {
- if (!this.isStopped && this._next) {
- var _parent = this._parent;
- if (!_parent.syncErrorThrowable) {
- this.__tryOrUnsub(this._next, value);
- }
- else if (this.__tryOrSetError(_parent, this._next, value)) {
- this.unsubscribe();
- }
- }
- };
- SafeSubscriber.prototype.error = function (err) {
- if (!this.isStopped) {
- var _parent = this._parent;
- if (this._error) {
- if (!_parent.syncErrorThrowable) {
- this.__tryOrUnsub(this._error, err);
- this.unsubscribe();
- }
- else {
- this.__tryOrSetError(_parent, this._error, err);
- this.unsubscribe();
- }
- }
- else if (!_parent.syncErrorThrowable) {
- this.unsubscribe();
- throw err;
- }
- else {
- _parent.syncErrorValue = err;
- _parent.syncErrorThrown = true;
- this.unsubscribe();
- }
- }
- };
- SafeSubscriber.prototype.complete = function () {
- if (!this.isStopped) {
- var _parent = this._parent;
- if (this._complete) {
- if (!_parent.syncErrorThrowable) {
- this.__tryOrUnsub(this._complete);
- this.unsubscribe();
- }
- else {
- this.__tryOrSetError(_parent, this._complete);
- this.unsubscribe();
- }
- }
- else {
- this.unsubscribe();
- }
- }
- };
- SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) {
- try {
- fn.call(this._context, value);
- }
- catch (err) {
- this.unsubscribe();
- throw err;
- }
- };
- SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) {
- try {
- fn.call(this._context, value);
- }
- catch (err) {
- parent.syncErrorValue = err;
- parent.syncErrorThrown = true;
- return true;
- }
- return false;
- };
- SafeSubscriber.prototype._unsubscribe = function () {
- var _parent = this._parent;
- this._context = null;
- this._parent = null;
- _parent.unsubscribe();
- };
- return SafeSubscriber;
- }(Subscriber));
- //# sourceMappingURL=Subscriber.js.map