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

materialize.ts 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Operator } from '../Operator';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { Notification } from '../Notification';
  5. import { OperatorFunction } from '../types';
  6. /**
  7. * Represents all of the notifications from the source Observable as `next`
  8. * emissions marked with their original types within {@link Notification}
  9. * objects.
  10. *
  11. * <span class="informal">Wraps `next`, `error` and `complete` emissions in
  12. * {@link Notification} objects, emitted as `next` on the output Observable.
  13. * </span>
  14. *
  15. * ![](materialize.png)
  16. *
  17. * `materialize` returns an Observable that emits a `next` notification for each
  18. * `next`, `error`, or `complete` emission of the source Observable. When the
  19. * source Observable emits `complete`, the output Observable will emit `next` as
  20. * a Notification of type "complete", and then it will emit `complete` as well.
  21. * When the source Observable emits `error`, the output will emit `next` as a
  22. * Notification of type "error", and then `complete`.
  23. *
  24. * This operator is useful for producing metadata of the source Observable, to
  25. * be consumed as `next` emissions. Use it in conjunction with
  26. * {@link dematerialize}.
  27. *
  28. * ## Example
  29. * Convert a faulty Observable to an Observable of Notifications
  30. * ```ts
  31. * import { of } from 'rxjs';
  32. * import { materialize, map } from 'rxjs/operators';
  33. *
  34. * const letters = of('a', 'b', 13, 'd');
  35. * const upperCase = letters.pipe(map(x => x.toUpperCase()));
  36. * const materialized = upperCase.pipe(materialize());
  37. * materialized.subscribe(x => console.log(x));
  38. *
  39. * // Results in the following:
  40. * // - Notification {kind: "N", value: "A", error: undefined, hasValue: true}
  41. * // - Notification {kind: "N", value: "B", error: undefined, hasValue: true}
  42. * // - Notification {kind: "E", value: undefined, error: TypeError:
  43. * // x.toUpperCase is not a function at MapSubscriber.letters.map.x
  44. * // [as project] (http://1…, hasValue: false}
  45. * ```
  46. *
  47. * @see {@link Notification}
  48. * @see {@link dematerialize}
  49. *
  50. * @return {Observable<Notification<T>>} An Observable that emits
  51. * {@link Notification} objects that wrap the original emissions from the source
  52. * Observable with metadata.
  53. * @method materialize
  54. * @owner Observable
  55. */
  56. export function materialize<T>(): OperatorFunction<T, Notification<T>> {
  57. return function materializeOperatorFunction(source: Observable<T>) {
  58. return source.lift(new MaterializeOperator());
  59. };
  60. }
  61. class MaterializeOperator<T> implements Operator<T, Notification<T>> {
  62. call(subscriber: Subscriber<Notification<T>>, source: any): any {
  63. return source.subscribe(new MaterializeSubscriber(subscriber));
  64. }
  65. }
  66. /**
  67. * We need this JSDoc comment for affecting ESDoc.
  68. * @ignore
  69. * @extends {Ignored}
  70. */
  71. class MaterializeSubscriber<T> extends Subscriber<T> {
  72. constructor(destination: Subscriber<Notification<T>>) {
  73. super(destination);
  74. }
  75. protected _next(value: T) {
  76. this.destination.next(Notification.createNext(value));
  77. }
  78. protected _error(err: any) {
  79. const destination = this.destination;
  80. destination.next(Notification.createError(err));
  81. destination.complete();
  82. }
  83. protected _complete() {
  84. const destination = this.destination;
  85. destination.next(Notification.createComplete());
  86. destination.complete();
  87. }
  88. }