Subscriber.js 10 KB

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