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 | * Converts an Observable of {@link Notification} objects into the emissions
|
---|
9 | * that they represent.
|
---|
10 | *
|
---|
11 | * <span class="informal">Unwraps {@link Notification} objects as actual `next`,
|
---|
12 | * `error` and `complete` emissions. The opposite of {@link materialize}.</span>
|
---|
13 | *
|
---|
14 | * ![](dematerialize.png)
|
---|
15 | *
|
---|
16 | * `dematerialize` is assumed to operate an Observable that only emits
|
---|
17 | * {@link Notification} objects as `next` emissions, and does not emit any
|
---|
18 | * `error`. Such Observable is the output of a `materialize` operation. Those
|
---|
19 | * notifications are then unwrapped using the metadata they contain, and emitted
|
---|
20 | * as `next`, `error`, and `complete` on the output Observable.
|
---|
21 | *
|
---|
22 | * Use this operator in conjunction with {@link materialize}.
|
---|
23 | *
|
---|
24 | * ## Example
|
---|
25 | * Convert an Observable of Notifications to an actual Observable
|
---|
26 | * ```ts
|
---|
27 | * import { of, Notification } from 'rxjs';
|
---|
28 | * import { dematerialize } from 'rxjs/operators';
|
---|
29 | *
|
---|
30 | * const notifA = new Notification('N', 'A');
|
---|
31 | * const notifB = new Notification('N', 'B');
|
---|
32 | * const notifE = new Notification('E', undefined,
|
---|
33 | * new TypeError('x.toUpperCase is not a function')
|
---|
34 | * );
|
---|
35 | * const materialized = of(notifA, notifB, notifE);
|
---|
36 | * const upperCase = materialized.pipe(dematerialize());
|
---|
37 | * upperCase.subscribe(x => console.log(x), e => console.error(e));
|
---|
38 | *
|
---|
39 | * // Results in:
|
---|
40 | * // A
|
---|
41 | * // B
|
---|
42 | * // TypeError: x.toUpperCase is not a function
|
---|
43 | * ```
|
---|
44 | *
|
---|
45 | * @see {@link Notification}
|
---|
46 | * @see {@link materialize}
|
---|
47 | *
|
---|
48 | * @return {Observable} An Observable that emits items and notifications
|
---|
49 | * embedded in Notification objects emitted by the source Observable.
|
---|
50 | * @method dematerialize
|
---|
51 | * @owner Observable
|
---|
52 | */
|
---|
53 | export function dematerialize<T>(): OperatorFunction<Notification<T>, T> {
|
---|
54 | return function dematerializeOperatorFunction(source: Observable<Notification<T>>) {
|
---|
55 | return source.lift(new DeMaterializeOperator());
|
---|
56 | };
|
---|
57 | }
|
---|
58 |
|
---|
59 | class DeMaterializeOperator<T extends Notification<any>, R> implements Operator<T, R> {
|
---|
60 | call(subscriber: Subscriber<any>, source: any): any {
|
---|
61 | return source.subscribe(new DeMaterializeSubscriber(subscriber));
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * We need this JSDoc comment for affecting ESDoc.
|
---|
67 | * @ignore
|
---|
68 | * @extends {Ignored}
|
---|
69 | */
|
---|
70 | class DeMaterializeSubscriber<T extends Notification<any>> extends Subscriber<T> {
|
---|
71 | constructor(destination: Subscriber<any>) {
|
---|
72 | super(destination);
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected _next(value: T) {
|
---|
76 | value.observe(this.destination);
|
---|
77 | }
|
---|
78 | }
|
---|