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