Subscriber.js 7.2 KB

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