dematerialize.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Subscriber } from '../Subscriber';
  2. /**
  3. * Converts an Observable of {@link Notification} objects into the emissions
  4. * that they represent.
  5. *
  6. * <span class="informal">Unwraps {@link Notification} objects as actual `next`,
  7. * `error` and `complete` emissions. The opposite of {@link materialize}.</span>
  8. *
  9. * <img src="./img/dematerialize.png" width="100%">
  10. *
  11. * `dematerialize` is assumed to operate an Observable that only emits
  12. * {@link Notification} objects as `next` emissions, and does not emit any
  13. * `error`. Such Observable is the output of a `materialize` operation. Those
  14. * notifications are then unwrapped using the metadata they contain, and emitted
  15. * as `next`, `error`, and `complete` on the output Observable.
  16. *
  17. * Use this operator in conjunction with {@link materialize}.
  18. *
  19. * @example <caption>Convert an Observable of Notifications to an actual Observable</caption>
  20. * var notifA = new Rx.Notification('N', 'A');
  21. * var notifB = new Rx.Notification('N', 'B');
  22. * var notifE = new Rx.Notification('E', void 0,
  23. * new TypeError('x.toUpperCase is not a function')
  24. * );
  25. * var materialized = Rx.Observable.of(notifA, notifB, notifE);
  26. * var upperCase = materialized.dematerialize();
  27. * upperCase.subscribe(x => console.log(x), e => console.error(e));
  28. *
  29. * // Results in:
  30. * // A
  31. * // B
  32. * // TypeError: x.toUpperCase is not a function
  33. *
  34. * @see {@link Notification}
  35. * @see {@link materialize}
  36. *
  37. * @return {Observable} An Observable that emits items and notifications
  38. * embedded in Notification objects emitted by the source Observable.
  39. * @method dematerialize
  40. * @owner Observable
  41. */
  42. export function dematerialize() {
  43. return function dematerializeOperatorFunction(source) {
  44. return source.lift(new DeMaterializeOperator());
  45. };
  46. }
  47. class DeMaterializeOperator {
  48. call(subscriber, source) {
  49. return source.subscribe(new DeMaterializeSubscriber(subscriber));
  50. }
  51. }
  52. /**
  53. * We need this JSDoc comment for affecting ESDoc.
  54. * @ignore
  55. * @extends {Ignored}
  56. */
  57. class DeMaterializeSubscriber extends Subscriber {
  58. constructor(destination) {
  59. super(destination);
  60. }
  61. _next(value) {
  62. value.observe(this.destination);
  63. }
  64. }
  65. //# sourceMappingURL=dematerialize.js.map