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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import { isFunction } from './util/isFunction';
  2. import { empty as emptyObserver } from './Observer';
  3. import { Observer, PartialObserver, TeardownLogic } from './types';
  4. import { Subscription } from './Subscription';
  5. import { rxSubscriber as rxSubscriberSymbol } from '../internal/symbol/rxSubscriber';
  6. import { config } from './config';
  7. import { hostReportError } from './util/hostReportError';
  8. /**
  9. * Implements the {@link Observer} interface and extends the
  10. * {@link Subscription} class. While the {@link Observer} is the public API for
  11. * consuming the values of an {@link Observable}, all Observers get converted to
  12. * a Subscriber, in order to provide Subscription-like capabilities such as
  13. * `unsubscribe`. Subscriber is a common type in RxJS, and crucial for
  14. * implementing operators, but it is rarely used as a public API.
  15. *
  16. * @class Subscriber<T>
  17. */
  18. export class Subscriber<T> extends Subscription implements Observer<T> {
  19. [rxSubscriberSymbol]() { return this; }
  20. /**
  21. * A static factory for a Subscriber, given a (potentially partial) definition
  22. * of an Observer.
  23. * @param {function(x: ?T): void} [next] The `next` callback of an Observer.
  24. * @param {function(e: ?any): void} [error] The `error` callback of an
  25. * Observer.
  26. * @param {function(): void} [complete] The `complete` callback of an
  27. * Observer.
  28. * @return {Subscriber<T>} A Subscriber wrapping the (partially defined)
  29. * Observer represented by the given arguments.
  30. * @nocollapse
  31. */
  32. static create<T>(next?: (x?: T) => void,
  33. error?: (e?: any) => void,
  34. complete?: () => void): Subscriber<T> {
  35. const subscriber = new Subscriber(next, error, complete);
  36. subscriber.syncErrorThrowable = false;
  37. return subscriber;
  38. }
  39. /** @internal */ syncErrorValue: any = null;
  40. /** @internal */ syncErrorThrown: boolean = false;
  41. /** @internal */ syncErrorThrowable: boolean = false;
  42. protected isStopped: boolean = false;
  43. protected destination: PartialObserver<any> | Subscriber<any>; // this `any` is the escape hatch to erase extra type param (e.g. R)
  44. /**
  45. * @param {Observer|function(value: T): void} [destinationOrNext] A partially
  46. * defined Observer or a `next` callback function.
  47. * @param {function(e: ?any): void} [error] The `error` callback of an
  48. * Observer.
  49. * @param {function(): void} [complete] The `complete` callback of an
  50. * Observer.
  51. */
  52. constructor(destinationOrNext?: PartialObserver<any> | ((value: T) => void),
  53. error?: (e?: any) => void,
  54. complete?: () => void) {
  55. super();
  56. switch (arguments.length) {
  57. case 0:
  58. this.destination = emptyObserver;
  59. break;
  60. case 1:
  61. if (!destinationOrNext) {
  62. this.destination = emptyObserver;
  63. break;
  64. }
  65. if (typeof destinationOrNext === 'object') {
  66. if (destinationOrNext instanceof Subscriber) {
  67. this.syncErrorThrowable = destinationOrNext.syncErrorThrowable;
  68. this.destination = destinationOrNext;
  69. destinationOrNext.add(this);
  70. } else {
  71. this.syncErrorThrowable = true;
  72. this.destination = new SafeSubscriber<T>(this, <PartialObserver<any>> destinationOrNext);
  73. }
  74. break;
  75. }
  76. default:
  77. this.syncErrorThrowable = true;
  78. this.destination = new SafeSubscriber<T>(this, <((value: T) => void)> destinationOrNext, error, complete);
  79. break;
  80. }
  81. }
  82. /**
  83. * The {@link Observer} callback to receive notifications of type `next` from
  84. * the Observable, with a value. The Observable may call this method 0 or more
  85. * times.
  86. * @param {T} [value] The `next` value.
  87. * @return {void}
  88. */
  89. next(value?: T): void {
  90. if (!this.isStopped) {
  91. this._next(value);
  92. }
  93. }
  94. /**
  95. * The {@link Observer} callback to receive notifications of type `error` from
  96. * the Observable, with an attached `Error`. Notifies the Observer that
  97. * the Observable has experienced an error condition.
  98. * @param {any} [err] The `error` exception.
  99. * @return {void}
  100. */
  101. error(err?: any): void {
  102. if (!this.isStopped) {
  103. this.isStopped = true;
  104. this._error(err);
  105. }
  106. }
  107. /**
  108. * The {@link Observer} callback to receive a valueless notification of type
  109. * `complete` from the Observable. Notifies the Observer that the Observable
  110. * has finished sending push-based notifications.
  111. * @return {void}
  112. */
  113. complete(): void {
  114. if (!this.isStopped) {
  115. this.isStopped = true;
  116. this._complete();
  117. }
  118. }
  119. unsubscribe(): void {
  120. if (this.closed) {
  121. return;
  122. }
  123. this.isStopped = true;
  124. super.unsubscribe();
  125. }
  126. protected _next(value: T): void {
  127. this.destination.next(value);
  128. }
  129. protected _error(err: any): void {
  130. this.destination.error(err);
  131. this.unsubscribe();
  132. }
  133. protected _complete(): void {
  134. this.destination.complete();
  135. this.unsubscribe();
  136. }
  137. /** @deprecated This is an internal implementation detail, do not use. */
  138. _unsubscribeAndRecycle(): Subscriber<T> {
  139. const { _parentOrParents } = this;
  140. this._parentOrParents = null;
  141. this.unsubscribe();
  142. this.closed = false;
  143. this.isStopped = false;
  144. this._parentOrParents = _parentOrParents;
  145. return this;
  146. }
  147. }
  148. /**
  149. * We need this JSDoc comment for affecting ESDoc.
  150. * @ignore
  151. * @extends {Ignored}
  152. */
  153. export class SafeSubscriber<T> extends Subscriber<T> {
  154. private _context: any;
  155. constructor(private _parentSubscriber: Subscriber<T>,
  156. observerOrNext?: PartialObserver<T> | ((value: T) => void),
  157. error?: (e?: any) => void,
  158. complete?: () => void) {
  159. super();
  160. let next: ((value: T) => void);
  161. let context: any = this;
  162. if (isFunction(observerOrNext)) {
  163. next = (<((value: T) => void)> observerOrNext);
  164. } else if (observerOrNext) {
  165. next = (<PartialObserver<T>> observerOrNext).next;
  166. error = (<PartialObserver<T>> observerOrNext).error;
  167. complete = (<PartialObserver<T>> observerOrNext).complete;
  168. if (observerOrNext !== emptyObserver) {
  169. context = Object.create(observerOrNext);
  170. if (isFunction(context.unsubscribe)) {
  171. this.add(<() => void> context.unsubscribe.bind(context));
  172. }
  173. context.unsubscribe = this.unsubscribe.bind(this);
  174. }
  175. }
  176. this._context = context;
  177. this._next = next;
  178. this._error = error;
  179. this._complete = complete;
  180. }
  181. next(value?: T): void {
  182. if (!this.isStopped && this._next) {
  183. const { _parentSubscriber } = this;
  184. if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  185. this.__tryOrUnsub(this._next, value);
  186. } else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
  187. this.unsubscribe();
  188. }
  189. }
  190. }
  191. error(err?: any): void {
  192. if (!this.isStopped) {
  193. const { _parentSubscriber } = this;
  194. const { useDeprecatedSynchronousErrorHandling } = config;
  195. if (this._error) {
  196. if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  197. this.__tryOrUnsub(this._error, err);
  198. this.unsubscribe();
  199. } else {
  200. this.__tryOrSetError(_parentSubscriber, this._error, err);
  201. this.unsubscribe();
  202. }
  203. } else if (!_parentSubscriber.syncErrorThrowable) {
  204. this.unsubscribe();
  205. if (useDeprecatedSynchronousErrorHandling) {
  206. throw err;
  207. }
  208. hostReportError(err);
  209. } else {
  210. if (useDeprecatedSynchronousErrorHandling) {
  211. _parentSubscriber.syncErrorValue = err;
  212. _parentSubscriber.syncErrorThrown = true;
  213. } else {
  214. hostReportError(err);
  215. }
  216. this.unsubscribe();
  217. }
  218. }
  219. }
  220. complete(): void {
  221. if (!this.isStopped) {
  222. const { _parentSubscriber } = this;
  223. if (this._complete) {
  224. const wrappedComplete = () => this._complete.call(this._context);
  225. if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  226. this.__tryOrUnsub(wrappedComplete);
  227. this.unsubscribe();
  228. } else {
  229. this.__tryOrSetError(_parentSubscriber, wrappedComplete);
  230. this.unsubscribe();
  231. }
  232. } else {
  233. this.unsubscribe();
  234. }
  235. }
  236. }
  237. private __tryOrUnsub(fn: Function, value?: any): void {
  238. try {
  239. fn.call(this._context, value);
  240. } catch (err) {
  241. this.unsubscribe();
  242. if (config.useDeprecatedSynchronousErrorHandling) {
  243. throw err;
  244. } else {
  245. hostReportError(err);
  246. }
  247. }
  248. }
  249. private __tryOrSetError(parent: Subscriber<T>, fn: Function, value?: any): boolean {
  250. if (!config.useDeprecatedSynchronousErrorHandling) {
  251. throw new Error('bad call');
  252. }
  253. try {
  254. fn.call(this._context, value);
  255. } catch (err) {
  256. if (config.useDeprecatedSynchronousErrorHandling) {
  257. parent.syncErrorValue = err;
  258. parent.syncErrorThrown = true;
  259. return true;
  260. } else {
  261. hostReportError(err);
  262. return true;
  263. }
  264. }
  265. return false;
  266. }
  267. /** @internal This is an internal implementation detail, do not use. */
  268. _unsubscribe(): void {
  269. const { _parentSubscriber } = this;
  270. this._context = null;
  271. this._parentSubscriber = null;
  272. _parentSubscriber.unsubscribe();
  273. }
  274. }