Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Subscriber.js 8.6KB

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