materialize.js 3.7KB

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