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';
|
---|
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 | * ![](materialize.png)
|
---|
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
|
---|
30 | * Convert a faulty Observable to an Observable of Notifications
|
---|
31 | * ```ts
|
---|
32 | * import { of } from 'rxjs';
|
---|
33 | * import { materialize, map } from 'rxjs/operators';
|
---|
34 | *
|
---|
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));
|
---|
39 | *
|
---|
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}
|
---|
46 | * ```
|
---|
47 | *
|
---|
48 | * @see {@link Notification}
|
---|
49 | * @see {@link dematerialize}
|
---|
50 | *
|
---|
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.
|
---|
54 | * @method materialize
|
---|
55 | * @owner Observable
|
---|
56 | */
|
---|
57 | export function materialize<T>(): OperatorFunction<T, Notification<T>> {
|
---|
58 | return function materializeOperatorFunction(source: Observable<T>) {
|
---|
59 | return source.lift(new MaterializeOperator());
|
---|
60 | };
|
---|
61 | }
|
---|
62 |
|
---|
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));
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * We need this JSDoc comment for affecting ESDoc.
|
---|
71 | * @ignore
|
---|
72 | * @extends {Ignored}
|
---|
73 | */
|
---|
74 | class MaterializeSubscriber<T> extends Subscriber<T> {
|
---|
75 | constructor(destination: Subscriber<Notification<T>>) {
|
---|
76 | super(destination);
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected _next(value: T) {
|
---|
80 | this.destination.next(Notification.createNext(value));
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected _error(err: any) {
|
---|
84 | const destination = this.destination;
|
---|
85 | destination.next(Notification.createError(err));
|
---|
86 | destination.complete();
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected _complete() {
|
---|
90 | const destination = this.destination;
|
---|
91 | destination.next(Notification.createComplete());
|
---|
92 | destination.complete();
|
---|
93 | }
|
---|
94 | }
|
---|