materialize.js 3.7 KB

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