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 * Converts an Observable of {@link Notification} objects into the emissions
11 * <span class="informal">Unwraps {@link Notification} objects as actual `next`,
12 * `error` and `complete` emissions. The opposite of {@link materialize}.</span>
14 * ![](dematerialize.png)
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.
22 * Use this operator in conjunction with {@link materialize}.
25 * Convert an Observable of Notifications to an actual Observable
27 * import { of, Notification } from 'rxjs';
28 * import { dematerialize } from 'rxjs/operators';
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')
35 * const materialized = of(notifA, notifB, notifE);
36 * const upperCase = materialized.pipe(dematerialize());
37 * upperCase.subscribe(x => console.log(x), e => console.error(e));
42 * // TypeError: x.toUpperCase is not a function
45 * @see {@link Notification}
46 * @see {@link materialize}
48 * @return {Observable} An Observable that emits items and notifications
49 * embedded in Notification objects emitted by the source Observable.
50 * @method dematerialize
53 export function dematerialize<T>(): OperatorFunction<Notification<T>, T> {
54 return function dematerializeOperatorFunction(source: Observable<Notification<T>>) {
55 return source.lift(new DeMaterializeOperator());
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));
66 * We need this JSDoc comment for affecting ESDoc.
70 class DeMaterializeSubscriber<T extends Notification<any>> extends Subscriber<T> {
71 constructor(destination: Subscriber<any>) {
75 protected _next(value: T) {
76 value.observe(this.destination);