Subscriber.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import { isFunction } from './util/isFunction';
  2. import { Observer, PartialObserver } from './Observer';
  3. import { Subscription } from './Subscription';
  4. import { empty as emptyObserver } from './Observer';
  5. import { rxSubscriber as rxSubscriberSymbol } from './symbol/rxSubscriber';
  6. /**
  7. * Implements the {@link Observer} interface and extends the
  8. * {@link Subscription} class. While the {@link Observer} is the public API for
  9. * consuming the values of an {@link Observable}, all Observers get converted to
  10. * a Subscriber, in order to provide Subscription-like capabilities such as
  11. * `unsubscribe`. Subscriber is a common type in RxJS, and crucial for
  12. * implementing operators, but it is rarely used as a public API.
  13. *
  14. * @class Subscriber<T>
  15. */
  16. export class Subscriber<T> extends Subscription implements Observer<T> {
  17. [rxSubscriberSymbol]() { return this; }
  18. /**
  19. * A static factory for a Subscriber, given a (potentially partial) definition
  20. * of an Observer.
  21. * @param {function(x: ?T): void} [next] The `next` callback of an Observer.
  22. * @param {function(e: ?any): void} [error] The `error` callback of an
  23. * Observer.
  24. * @param {function(): void} [complete] The `complete` callback of an
  25. * Observer.
  26. * @return {Subscriber<T>} A Subscriber wrapping the (partially defined)
  27. * Observer represented by the given arguments.
  28. */
  29. static create<T>(next?: (x?: T) => void,
  30. error?: (e?: any) => void,
  31. complete?: () => void): Subscriber<T> {
  32. const subscriber = new Subscriber(next, error, complete);
  33. subscriber.syncErrorThrowable = false;
  34. return subscriber;
  35. }
  36. public syncErrorValue: any = null;
  37. public syncErrorThrown: boolean = false;
  38. public syncErrorThrowable: boolean = false;
  39. protected isStopped: boolean = false;
  40. protected destination: PartialObserver<any>; // this `any` is the escape hatch to erase extra type param (e.g. R)
  41. /**
  42. * @param {Observer|function(value: T): void} [destinationOrNext] A partially
  43. * defined Observer or a `next` callback function.
  44. * @param {function(e: ?any): void} [error] The `error` callback of an
  45. * Observer.
  46. * @param {function(): void} [complete] The `complete` callback of an
  47. * Observer.
  48. */
  49. constructor(destinationOrNext?: PartialObserver<any> | ((value: T) => void),
  50. error?: (e?: any) => void,
  51. complete?: () => void) {
  52. super();
  53. switch (arguments.length) {
  54. case 0:
  55. this.destination = emptyObserver;
  56. break;
  57. case 1:
  58. if (!destinationOrNext) {
  59. this.destination = emptyObserver;
  60. break;
  61. }
  62. if (typeof destinationOrNext === 'object') {
  63. // HACK(benlesh): To resolve an issue where Node users may have multiple
  64. // copies of rxjs in their node_modules directory.
  65. if (isTrustedSubscriber(destinationOrNext)) {
  66. const trustedSubscriber = destinationOrNext[rxSubscriberSymbol]() as Subscriber<any>;
  67. this.syncErrorThrowable = trustedSubscriber.syncErrorThrowable;
  68. this.destination = trustedSubscriber;
  69. trustedSubscriber.add(this);
  70. } else {
  71. this.syncErrorThrowable = true;
  72. this.destination = new SafeSubscriber<T>(this, <PartialObserver<any>> destinationOrNext);
  73. }
  74. break;
  75. }
  76. default:
  77. this.syncErrorThrowable = true;
  78. this.destination = new SafeSubscriber<T>(this, <((value: T) => void)> destinationOrNext, error, complete);
  79. break;
  80. }
  81. }
  82. /**
  83. * The {@link Observer} callback to receive notifications of type `next` from
  84. * the Observable, with a value. The Observable may call this method 0 or more
  85. * times.
  86. * @param {T} [value] The `next` value.
  87. * @return {void}
  88. */
  89. next(value?: T): void {
  90. if (!this.isStopped) {
  91. this._next(value);
  92. }
  93. }
  94. /**
  95. * The {@link Observer} callback to receive notifications of type `error` from
  96. * the Observable, with an attached {@link Error}. Notifies the Observer that
  97. * the Observable has experienced an error condition.
  98. * @param {any} [err] The `error` exception.
  99. * @return {void}
  100. */
  101. error(err?: any): void {
  102. if (!this.isStopped) {
  103. this.isStopped = true;
  104. this._error(err);
  105. }
  106. }
  107. /**
  108. * The {@link Observer} callback to receive a valueless notification of type
  109. * `complete` from the Observable. Notifies the Observer that the Observable
  110. * has finished sending push-based notifications.
  111. * @return {void}
  112. */
  113. complete(): void {
  114. if (!this.isStopped) {
  115. this.isStopped = true;
  116. this._complete();
  117. }
  118. }
  119. unsubscribe(): void {
  120. if (this.closed) {
  121. return;
  122. }
  123. this.isStopped = true;
  124. super.unsubscribe();
  125. }
  126. protected _next(value: T): void {
  127. this.destination.next(value);
  128. }
  129. protected _error(err: any): void {
  130. this.destination.error(err);
  131. this.unsubscribe();
  132. }
  133. protected _complete(): void {
  134. this.destination.complete();
  135. this.unsubscribe();
  136. }
  137. /** @deprecated internal use only */ _unsubscribeAndRecycle(): Subscriber<T> {
  138. const { _parent, _parents } = this;
  139. this._parent = null;
  140. this._parents = null;
  141. this.unsubscribe();
  142. this.closed = false;
  143. this.isStopped = false;
  144. this._parent = _parent;
  145. this._parents = _parents;
  146. return this;
  147. }
  148. }
  149. /**
  150. * We need this JSDoc comment for affecting ESDoc.
  151. * @ignore
  152. * @extends {Ignored}
  153. */
  154. class SafeSubscriber<T> extends Subscriber<T> {
  155. private _context: any;
  156. constructor(private _parentSubscriber: Subscriber<T>,
  157. observerOrNext?: PartialObserver<T> | ((value: T) => void),
  158. error?: (e?: any) => void,
  159. complete?: () => void) {
  160. super();
  161. let next: ((value: T) => void);
  162. let context: any = this;
  163. if (isFunction(observerOrNext)) {
  164. next = (<((value: T) => void)> observerOrNext);
  165. } else if (observerOrNext) {
  166. next = (<PartialObserver<T>> observerOrNext).next;
  167. error = (<PartialObserver<T>> observerOrNext).error;
  168. complete = (<PartialObserver<T>> observerOrNext).complete;
  169. if (observerOrNext !== emptyObserver) {
  170. context = Object.create(observerOrNext);
  171. if (isFunction(context.unsubscribe)) {
  172. this.add(<() => void> context.unsubscribe.bind(context));
  173. }
  174. context.unsubscribe = this.unsubscribe.bind(this);
  175. }
  176. }
  177. this._context = context;
  178. this._next = next;
  179. this._error = error;
  180. this._complete = complete;
  181. }
  182. next(value?: T): void {
  183. if (!this.isStopped && this._next) {
  184. const { _parentSubscriber } = this;
  185. if (!_parentSubscriber.syncErrorThrowable) {
  186. this.__tryOrUnsub(this._next, value);
  187. } else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
  188. this.unsubscribe();
  189. }
  190. }
  191. }
  192. error(err?: any): void {
  193. if (!this.isStopped) {
  194. const { _parentSubscriber } = this;
  195. if (this._error) {
  196. if (!_parentSubscriber.syncErrorThrowable) {
  197. this.__tryOrUnsub(this._error, err);
  198. this.unsubscribe();
  199. } else {
  200. this.__tryOrSetError(_parentSubscriber, this._error, err);
  201. this.unsubscribe();
  202. }
  203. } else if (!_parentSubscriber.syncErrorThrowable) {
  204. this.unsubscribe();
  205. throw err;
  206. } else {
  207. _parentSubscriber.syncErrorValue = err;
  208. _parentSubscriber.syncErrorThrown = true;
  209. this.unsubscribe();
  210. }
  211. }
  212. }
  213. complete(): void {
  214. if (!this.isStopped) {
  215. const { _parentSubscriber } = this;
  216. if (this._complete) {
  217. const wrappedComplete = () => this._complete.call(this._context);
  218. if (!_parentSubscriber.syncErrorThrowable) {
  219. this.__tryOrUnsub(wrappedComplete);
  220. this.unsubscribe();
  221. } else {
  222. this.__tryOrSetError(_parentSubscriber, wrappedComplete);
  223. this.unsubscribe();
  224. }
  225. } else {
  226. this.unsubscribe();
  227. }
  228. }
  229. }
  230. private __tryOrUnsub(fn: Function, value?: any): void {
  231. try {
  232. fn.call(this._context, value);
  233. } catch (err) {
  234. this.unsubscribe();
  235. throw err;
  236. }
  237. }
  238. private __tryOrSetError(parent: Subscriber<T>, fn: Function, value?: any): boolean {
  239. try {
  240. fn.call(this._context, value);
  241. } catch (err) {
  242. parent.syncErrorValue = err;
  243. parent.syncErrorThrown = true;
  244. return true;
  245. }
  246. return false;
  247. }
  248. /** @deprecated internal use only */ _unsubscribe(): void {
  249. const { _parentSubscriber } = this;
  250. this._context = null;
  251. this._parentSubscriber = null;
  252. _parentSubscriber.unsubscribe();
  253. }
  254. }
  255. function isTrustedSubscriber(obj: any) {
  256. return obj instanceof Subscriber || ('syncErrorThrowable' in obj && obj[rxSubscriberSymbol]);
  257. }