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 7.3KB

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