materialize.js 3.0 KB

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