1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Notification } from '../Notification';
5 import { OperatorFunction } from '../types';
8 * Represents all of the notifications from the source Observable as `next`
9 * emissions marked with their original types within {@link Notification}
12 * <span class="informal">Wraps `next`, `error` and `complete` emissions in
13 * {@link Notification} objects, emitted as `next` on the output Observable.
16 * ![](materialize.png)
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`.
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}.
30 * Convert a faulty Observable to an Observable of Notifications
32 * import { of } from 'rxjs';
33 * import { materialize, map } from 'rxjs/operators';
35 * const letters = of('a', 'b', 13, 'd');
36 * const upperCase = letters.pipe(map(x => x.toUpperCase()));
37 * const materialized = upperCase.pipe(materialize());
38 * materialized.subscribe(x => console.log(x));
40 * // Results in the following:
41 * // - Notification {kind: "N", value: "A", error: undefined, hasValue: true}
42 * // - Notification {kind: "N", value: "B", error: undefined, hasValue: true}
43 * // - Notification {kind: "E", value: undefined, error: TypeError:
44 * // x.toUpperCase is not a function at MapSubscriber.letters.map.x
45 * // [as project] (http://1…, hasValue: false}
48 * @see {@link Notification}
49 * @see {@link dematerialize}
51 * @return {Observable<Notification<T>>} An Observable that emits
52 * {@link Notification} objects that wrap the original emissions from the source
53 * Observable with metadata.
57 export function materialize<T>(): OperatorFunction<T, Notification<T>> {
58 return function materializeOperatorFunction(source: Observable<T>) {
59 return source.lift(new MaterializeOperator());
63 class MaterializeOperator<T> implements Operator<T, Notification<T>> {
64 call(subscriber: Subscriber<Notification<T>>, source: any): any {
65 return source.subscribe(new MaterializeSubscriber(subscriber));
70 * We need this JSDoc comment for affecting ESDoc.
74 class MaterializeSubscriber<T> extends Subscriber<T> {
75 constructor(destination: Subscriber<Notification<T>>) {
79 protected _next(value: T) {
80 this.destination.next(Notification.createNext(value));
83 protected _error(err: any) {
84 const destination = this.destination;
85 destination.next(Notification.createError(err));
86 destination.complete();
89 protected _complete() {
90 const destination = this.destination;
91 destination.next(Notification.createComplete());
92 destination.complete();