Subscriber.js 8.8 KB

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