Subscriber.js 10 KB

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