Subscriber.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 _parentOrParents = this._parentOrParents;
  88. this._parentOrParents = null;
  89. this.unsubscribe();
  90. this.closed = false;
  91. this.isStopped = false;
  92. this._parentOrParents = _parentOrParents;
  93. return this;
  94. };
  95. return Subscriber;
  96. }(Subscription));
  97. export { Subscriber };
  98. var SafeSubscriber = /*@__PURE__*/ (function (_super) {
  99. tslib_1.__extends(SafeSubscriber, _super);
  100. function SafeSubscriber(_parentSubscriber, observerOrNext, error, complete) {
  101. var _this = _super.call(this) || this;
  102. _this._parentSubscriber = _parentSubscriber;
  103. var next;
  104. var context = _this;
  105. if (isFunction(observerOrNext)) {
  106. next = observerOrNext;
  107. }
  108. else if (observerOrNext) {
  109. next = observerOrNext.next;
  110. error = observerOrNext.error;
  111. complete = observerOrNext.complete;
  112. if (observerOrNext !== emptyObserver) {
  113. context = Object.create(observerOrNext);
  114. if (isFunction(context.unsubscribe)) {
  115. _this.add(context.unsubscribe.bind(context));
  116. }
  117. context.unsubscribe = _this.unsubscribe.bind(_this);
  118. }
  119. }
  120. _this._context = context;
  121. _this._next = next;
  122. _this._error = error;
  123. _this._complete = complete;
  124. return _this;
  125. }
  126. SafeSubscriber.prototype.next = function (value) {
  127. if (!this.isStopped && this._next) {
  128. var _parentSubscriber = this._parentSubscriber;
  129. if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  130. this.__tryOrUnsub(this._next, value);
  131. }
  132. else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
  133. this.unsubscribe();
  134. }
  135. }
  136. };
  137. SafeSubscriber.prototype.error = function (err) {
  138. if (!this.isStopped) {
  139. var _parentSubscriber = this._parentSubscriber;
  140. var useDeprecatedSynchronousErrorHandling = config.useDeprecatedSynchronousErrorHandling;
  141. if (this._error) {
  142. if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  143. this.__tryOrUnsub(this._error, err);
  144. this.unsubscribe();
  145. }
  146. else {
  147. this.__tryOrSetError(_parentSubscriber, this._error, err);
  148. this.unsubscribe();
  149. }
  150. }
  151. else if (!_parentSubscriber.syncErrorThrowable) {
  152. this.unsubscribe();
  153. if (useDeprecatedSynchronousErrorHandling) {
  154. throw err;
  155. }
  156. hostReportError(err);
  157. }
  158. else {
  159. if (useDeprecatedSynchronousErrorHandling) {
  160. _parentSubscriber.syncErrorValue = err;
  161. _parentSubscriber.syncErrorThrown = true;
  162. }
  163. else {
  164. hostReportError(err);
  165. }
  166. this.unsubscribe();
  167. }
  168. }
  169. };
  170. SafeSubscriber.prototype.complete = function () {
  171. var _this = this;
  172. if (!this.isStopped) {
  173. var _parentSubscriber = this._parentSubscriber;
  174. if (this._complete) {
  175. var wrappedComplete = function () { return _this._complete.call(_this._context); };
  176. if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  177. this.__tryOrUnsub(wrappedComplete);
  178. this.unsubscribe();
  179. }
  180. else {
  181. this.__tryOrSetError(_parentSubscriber, wrappedComplete);
  182. this.unsubscribe();
  183. }
  184. }
  185. else {
  186. this.unsubscribe();
  187. }
  188. }
  189. };
  190. SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) {
  191. try {
  192. fn.call(this._context, value);
  193. }
  194. catch (err) {
  195. this.unsubscribe();
  196. if (config.useDeprecatedSynchronousErrorHandling) {
  197. throw err;
  198. }
  199. else {
  200. hostReportError(err);
  201. }
  202. }
  203. };
  204. SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) {
  205. if (!config.useDeprecatedSynchronousErrorHandling) {
  206. throw new Error('bad call');
  207. }
  208. try {
  209. fn.call(this._context, value);
  210. }
  211. catch (err) {
  212. if (config.useDeprecatedSynchronousErrorHandling) {
  213. parent.syncErrorValue = err;
  214. parent.syncErrorThrown = true;
  215. return true;
  216. }
  217. else {
  218. hostReportError(err);
  219. return true;
  220. }
  221. }
  222. return false;
  223. };
  224. SafeSubscriber.prototype._unsubscribe = function () {
  225. var _parentSubscriber = this._parentSubscriber;
  226. this._context = null;
  227. this._parentSubscriber = null;
  228. _parentSubscriber.unsubscribe();
  229. };
  230. return SafeSubscriber;
  231. }(Subscriber));
  232. export { SafeSubscriber };
  233. //# sourceMappingURL=Subscriber.js.map