Subscriber.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /** PURE_IMPORTS_START tslib,_util_isFunction,_Observer,_Subscription,_internal_symbol_rxSubscriber,_config,_util_hostReportError PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { isFunction } from './util/isFunction';
  4. import { empty as emptyObserver } from './Observer';
  5. import { Subscription } from './Subscription';
  6. import { rxSubscriber as rxSubscriberSymbol } from '../internal/symbol/rxSubscriber';
  7. import { config } from './config';
  8. import { hostReportError } from './util/hostReportError';
  9. var Subscriber = /*@__PURE__*/ (function (_super) {
  10. tslib_1.__extends(Subscriber, _super);
  11. function Subscriber(destinationOrNext, error, complete) {
  12. var _this = _super.call(this) || this;
  13. _this.syncErrorValue = null;
  14. _this.syncErrorThrown = false;
  15. _this.syncErrorThrowable = false;
  16. _this.isStopped = false;
  17. switch (arguments.length) {
  18. case 0:
  19. _this.destination = emptyObserver;
  20. break;
  21. case 1:
  22. if (!destinationOrNext) {
  23. _this.destination = emptyObserver;
  24. break;
  25. }
  26. if (typeof destinationOrNext === 'object') {
  27. if (destinationOrNext instanceof Subscriber) {
  28. _this.syncErrorThrowable = destinationOrNext.syncErrorThrowable;
  29. _this.destination = destinationOrNext;
  30. destinationOrNext.add(_this);
  31. }
  32. else {
  33. _this.syncErrorThrowable = true;
  34. _this.destination = new SafeSubscriber(_this, destinationOrNext);
  35. }
  36. break;
  37. }
  38. default:
  39. _this.syncErrorThrowable = true;
  40. _this.destination = new SafeSubscriber(_this, destinationOrNext, error, complete);
  41. break;
  42. }
  43. return _this;
  44. }
  45. Subscriber.prototype[rxSubscriberSymbol] = function () { return this; };
  46. Subscriber.create = function (next, error, complete) {
  47. var subscriber = new Subscriber(next, error, complete);
  48. subscriber.syncErrorThrowable = false;
  49. return subscriber;
  50. };
  51. Subscriber.prototype.next = function (value) {
  52. if (!this.isStopped) {
  53. this._next(value);
  54. }
  55. };
  56. Subscriber.prototype.error = function (err) {
  57. if (!this.isStopped) {
  58. this.isStopped = true;
  59. this._error(err);
  60. }
  61. };
  62. Subscriber.prototype.complete = function () {
  63. if (!this.isStopped) {
  64. this.isStopped = true;
  65. this._complete();
  66. }
  67. };
  68. Subscriber.prototype.unsubscribe = function () {
  69. if (this.closed) {
  70. return;
  71. }
  72. this.isStopped = true;
  73. _super.prototype.unsubscribe.call(this);
  74. };
  75. Subscriber.prototype._next = function (value) {
  76. this.destination.next(value);
  77. };
  78. Subscriber.prototype._error = function (err) {
  79. this.destination.error(err);
  80. this.unsubscribe();
  81. };
  82. Subscriber.prototype._complete = function () {
  83. this.destination.complete();
  84. this.unsubscribe();
  85. };
  86. Subscriber.prototype._unsubscribeAndRecycle = function () {
  87. var _a = this, _parent = _a._parent, _parents = _a._parents;
  88. this._parent = null;
  89. this._parents = null;
  90. this.unsubscribe();
  91. this.closed = false;
  92. this.isStopped = false;
  93. this._parent = _parent;
  94. this._parents = _parents;
  95. return this;
  96. };
  97. return Subscriber;
  98. }(Subscription));
  99. export { Subscriber };
  100. var SafeSubscriber = /*@__PURE__*/ (function (_super) {
  101. tslib_1.__extends(SafeSubscriber, _super);
  102. function SafeSubscriber(_parentSubscriber, observerOrNext, error, complete) {
  103. var _this = _super.call(this) || this;
  104. _this._parentSubscriber = _parentSubscriber;
  105. var next;
  106. var context = _this;
  107. if (isFunction(observerOrNext)) {
  108. next = observerOrNext;
  109. }
  110. else if (observerOrNext) {
  111. next = observerOrNext.next;
  112. error = observerOrNext.error;
  113. complete = observerOrNext.complete;
  114. if (observerOrNext !== emptyObserver) {
  115. context = Object.create(observerOrNext);
  116. if (isFunction(context.unsubscribe)) {
  117. _this.add(context.unsubscribe.bind(context));
  118. }
  119. context.unsubscribe = _this.unsubscribe.bind(_this);
  120. }
  121. }
  122. _this._context = context;
  123. _this._next = next;
  124. _this._error = error;
  125. _this._complete = complete;
  126. return _this;
  127. }
  128. SafeSubscriber.prototype.next = function (value) {
  129. if (!this.isStopped && this._next) {
  130. var _parentSubscriber = this._parentSubscriber;
  131. if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  132. this.__tryOrUnsub(this._next, value);
  133. }
  134. else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
  135. this.unsubscribe();
  136. }
  137. }
  138. };
  139. SafeSubscriber.prototype.error = function (err) {
  140. if (!this.isStopped) {
  141. var _parentSubscriber = this._parentSubscriber;
  142. var useDeprecatedSynchronousErrorHandling = config.useDeprecatedSynchronousErrorHandling;
  143. if (this._error) {
  144. if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  145. this.__tryOrUnsub(this._error, err);
  146. this.unsubscribe();
  147. }
  148. else {
  149. this.__tryOrSetError(_parentSubscriber, this._error, err);
  150. this.unsubscribe();
  151. }
  152. }
  153. else if (!_parentSubscriber.syncErrorThrowable) {
  154. this.unsubscribe();
  155. if (useDeprecatedSynchronousErrorHandling) {
  156. throw err;
  157. }
  158. hostReportError(err);
  159. }
  160. else {
  161. if (useDeprecatedSynchronousErrorHandling) {
  162. _parentSubscriber.syncErrorValue = err;
  163. _parentSubscriber.syncErrorThrown = true;
  164. }
  165. else {
  166. hostReportError(err);
  167. }
  168. this.unsubscribe();
  169. }
  170. }
  171. };
  172. SafeSubscriber.prototype.complete = function () {
  173. var _this = this;
  174. if (!this.isStopped) {
  175. var _parentSubscriber = this._parentSubscriber;
  176. if (this._complete) {
  177. var wrappedComplete = function () { return _this._complete.call(_this._context); };
  178. if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  179. this.__tryOrUnsub(wrappedComplete);
  180. this.unsubscribe();
  181. }
  182. else {
  183. this.__tryOrSetError(_parentSubscriber, wrappedComplete);
  184. this.unsubscribe();
  185. }
  186. }
  187. else {
  188. this.unsubscribe();
  189. }
  190. }
  191. };
  192. SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) {
  193. try {
  194. fn.call(this._context, value);
  195. }
  196. catch (err) {
  197. this.unsubscribe();
  198. if (config.useDeprecatedSynchronousErrorHandling) {
  199. throw err;
  200. }
  201. else {
  202. hostReportError(err);
  203. }
  204. }
  205. };
  206. SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) {
  207. if (!config.useDeprecatedSynchronousErrorHandling) {
  208. throw new Error('bad call');
  209. }
  210. try {
  211. fn.call(this._context, value);
  212. }
  213. catch (err) {
  214. if (config.useDeprecatedSynchronousErrorHandling) {
  215. parent.syncErrorValue = err;
  216. parent.syncErrorThrown = true;
  217. return true;
  218. }
  219. else {
  220. hostReportError(err);
  221. return true;
  222. }
  223. }
  224. return false;
  225. };
  226. SafeSubscriber.prototype._unsubscribe = function () {
  227. var _parentSubscriber = this._parentSubscriber;
  228. this._context = null;
  229. this._parentSubscriber = null;
  230. _parentSubscriber.unsubscribe();
  231. };
  232. return SafeSubscriber;
  233. }(Subscriber));
  234. export { SafeSubscriber };
  235. //# sourceMappingURL=Subscriber.js.map