Debian Codegraph @

node/rxjs/src/internal/operators/dematerialize.ts

raw
1 import { observeNotification } from '../Notification';
2 import { OperatorFunction, ObservableNotification, ValueFromNotification } from '../types';
3 import { operate } from '../util/lift';
4 import { createOperatorSubscriber } from './OperatorSubscriber';
5
6 /**
7 * Converts an Observable of {@link ObservableNotification} objects into the emissions
8 * that they represent.
9 *
10 * <span class="informal">Unwraps {@link ObservableNotification} objects as actual `next`,
11 * `error` and `complete` emissions. The opposite of {@link materialize}.</span>
12 *
13 * ![](dematerialize.png)
14 *
15 * `dematerialize` is assumed to operate an Observable that only emits
16 * {@link ObservableNotification} objects as `next` emissions, and does not emit any
17 * `error`. Such Observable is the output of a `materialize` operation. Those
18 * notifications are then unwrapped using the metadata they contain, and emitted
19 * as `next`, `error`, and `complete` on the output Observable.
20 *
21 * Use this operator in conjunction with {@link materialize}.
22 *
23 * ## Example
24 *
25 * Convert an Observable of Notifications to an actual Observable
26 *
27 * ```ts
28 * import { NextNotification, ErrorNotification, of, dematerialize } from 'rxjs';
29 *
30 * const notifA: NextNotification<string> = { kind: 'N', value: 'A' };
31 * const notifB: NextNotification<string> = { kind: 'N', value: 'B' };
32 * const notifE: ErrorNotification = { kind: 'E', error: new TypeError('x.toUpperCase is not a function') };
33 *
34 * const materialized = of(notifA, notifB, notifE);
35 *
36 * const upperCase = materialized.pipe(dematerialize());
37 * upperCase.subscribe({
38 * next: x => console.log(x),
39 * error: e => console.error(e)
40 * });
41 *
42 * // Results in:
43 * // A
44 * // B
45 * // TypeError: x.toUpperCase is not a function
46 * ```
47 *
48 * @see {@link materialize}
49 *
50 * @return A function that returns an Observable that emits items and
51 * notifications embedded in Notification objects emitted by the source
52 * Observable.
53 */
54 export function dematerialize<N extends ObservableNotification<any>>(): OperatorFunction<N, ValueFromNotification<N>> {
55 return operate((source, subscriber) => {
56 source.subscribe(createOperatorSubscriber(subscriber, (notification) => observeNotification(notification, subscriber)));
57 });
58 }