Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

Subscriber.js 7.2KB

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