UNPKG

2.24 kBPlain TextView Raw
1
2import { Observable, Notification } from 'rxjs';
3import { materialize as higherOrder } from 'rxjs/operators';
4
5/**
6 * Represents all of the notifications from the source Observable as `next`
7 * emissions marked with their original types within {@link Notification}
8 * objects.
9 *
10 * <span class="informal">Wraps `next`, `error` and `complete` emissions in
11 * {@link Notification} objects, emitted as `next` on the output Observable.
12 * </span>
13 *
14 * <img src="./img/materialize.png" width="100%">
15 *
16 * `materialize` returns an Observable that emits a `next` notification for each
17 * `next`, `error`, or `complete` emission of the source Observable. When the
18 * source Observable emits `complete`, the output Observable will emit `next` as
19 * a Notification of type "complete", and then it will emit `complete` as well.
20 * When the source Observable emits `error`, the output will emit `next` as a
21 * Notification of type "error", and then `complete`.
22 *
23 * This operator is useful for producing metadata of the source Observable, to
24 * be consumed as `next` emissions. Use it in conjunction with
25 * {@link dematerialize}.
26 *
27 * @example <caption>Convert a faulty Observable to an Observable of Notifications</caption>
28 * var letters = Rx.Observable.of('a', 'b', 13, 'd');
29 * var upperCase = letters.map(x => x.toUpperCase());
30 * var materialized = upperCase.materialize();
31 * materialized.subscribe(x => console.log(x));
32 *
33 * // Results in the following:
34 * // - Notification {kind: "N", value: "A", error: undefined, hasValue: true}
35 * // - Notification {kind: "N", value: "B", error: undefined, hasValue: true}
36 * // - Notification {kind: "E", value: undefined, error: TypeError:
37 * // x.toUpperCase is not a function at MapSubscriber.letters.map.x
38 * // [as project] (http://1…, hasValue: false}
39 *
40 * @see {@link Notification}
41 * @see {@link dematerialize}
42 *
43 * @return {Observable<Notification<T>>} An Observable that emits
44 * {@link Notification} objects that wrap the original emissions from the source
45 * Observable with metadata.
46 * @method materialize
47 * @owner Observable
48 */
49export function materialize<T>(this: Observable<T>): Observable<Notification<T>> {
50 return higherOrder()(this) as Observable<Notification<T>>;
51}