9ecfa3fb444ab5ffc5f061ab5fb4e757014d93f04dd2401bea3293dc773287ff2dcac4c0881d5c7606c6d391eb0a2ca2bf02d9b1bce9832d56a98a4f21e2cf 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Notification } from '../Notification';
  2. import { OperatorFunction, ObservableNotification } from '../types';
  3. import { operate } from '../util/lift';
  4. import { createOperatorSubscriber } from './OperatorSubscriber';
  5. /**
  6. * Represents all of the notifications from the source Observable as `next`
  7. * emissions marked with their original types within {@link Notification}
  8. * objects.
  9. *
  10. * <span class="informal">Wraps `next`, `error` and `complete` emissions in
  11. * {@link Notification} objects, emitted as `next` on the output Observable.
  12. * </span>
  13. *
  14. * ![](materialize.png)
  15. *
  16. * `materialize` returns an Observable that emits a `next` notification for each
  17. * `next`, `error`, or `complete` emission of the source Observable. When the
  18. * source Observable emits `complete`, the output Observable will emit `next` as
  19. * a Notification of type "complete", and then it will emit `complete` as well.
  20. * When the source Observable emits `error`, the output will emit `next` as a
  21. * Notification of type "error", and then `complete`.
  22. *
  23. * This operator is useful for producing metadata of the source Observable, to
  24. * be consumed as `next` emissions. Use it in conjunction with
  25. * {@link dematerialize}.
  26. *
  27. * ## Example
  28. *
  29. * Convert a faulty Observable to an Observable of Notifications
  30. *
  31. * ```ts
  32. * import { of, materialize, map } from 'rxjs';
  33. *
  34. * const letters = of('a', 'b', 13, 'd');
  35. * const upperCase = letters.pipe(map((x: any) => x.toUpperCase()));
  36. * const materialized = upperCase.pipe(materialize());
  37. *
  38. * materialized.subscribe(x => console.log(x));
  39. *
  40. * // Results in the following:
  41. * // - Notification { kind: 'N', value: 'A', error: undefined, hasValue: true }
  42. * // - Notification { kind: 'N', value: 'B', error: undefined, hasValue: true }
  43. * // - Notification { kind: 'E', value: undefined, error: TypeError { message: x.toUpperCase is not a function }, hasValue: false }
  44. * ```
  45. *
  46. * @see {@link Notification}
  47. * @see {@link dematerialize}
  48. *
  49. * @return A function that returns an Observable that emits
  50. * {@link Notification} objects that wrap the original emissions from the
  51. * source Observable with metadata.
  52. */
  53. export function materialize<T>(): OperatorFunction<T, Notification<T> & ObservableNotification<T>> {
  54. return operate((source, subscriber) => {
  55. source.subscribe(
  56. createOperatorSubscriber(
  57. subscriber,
  58. (value) => {
  59. subscriber.next(Notification.createNext(value));
  60. },
  61. () => {
  62. subscriber.next(Notification.createComplete());
  63. subscriber.complete();
  64. },
  65. (err) => {
  66. subscriber.next(Notification.createError(err));
  67. subscriber.complete();
  68. }
  69. )
  70. );
  71. });
  72. }