| 1 |
import { Notification } from '../Notification'; |
| 2 |
import { OperatorFunction, ObservableNotification } from '../types'; |
| 3 |
import { operate } from '../util/lift'; |
| 4 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Represents all of the notifications from the source Observable as `next` |
| 8 |
* emissions marked with their original types within {@link Notification} |
| 9 |
* objects. |
| 10 |
* |
| 11 |
* <span class="informal">Wraps `next`, `error` and `complete` emissions in |
| 12 |
* {@link Notification} objects, emitted as `next` on the output Observable. |
| 13 |
* </span> |
| 14 |
* |
| 15 |
*  |
| 16 |
* |
| 17 |
* `materialize` returns an Observable that emits a `next` notification for each |
| 18 |
* `next`, `error`, or `complete` emission of the source Observable. When the |
| 19 |
* source Observable emits `complete`, the output Observable will emit `next` as |
| 20 |
* a Notification of type "complete", and then it will emit `complete` as well. |
| 21 |
* When the source Observable emits `error`, the output will emit `next` as a |
| 22 |
* Notification of type "error", and then `complete`. |
| 23 |
* |
| 24 |
* This operator is useful for producing metadata of the source Observable, to |
| 25 |
* be consumed as `next` emissions. Use it in conjunction with |
| 26 |
* {@link dematerialize}. |
| 27 |
* |
| 28 |
* ## Example |
| 29 |
* |
| 30 |
* Convert a faulty Observable to an Observable of Notifications |
| 31 |
* |
| 32 |
* ```ts |
| 33 |
* import { of, materialize, map } from 'rxjs'; |
| 34 |
* |
| 35 |
* const letters = of('a', 'b', 13, 'd'); |
| 36 |
* const upperCase = letters.pipe(map((x: any) => x.toUpperCase())); |
| 37 |
* const materialized = upperCase.pipe(materialize()); |
| 38 |
* |
| 39 |
* materialized.subscribe(x => console.log(x)); |
| 40 |
* |
| 41 |
* // Results in the following: |
| 42 |
* // - Notification { kind: 'N', value: 'A', error: undefined, hasValue: true } |
| 43 |
* // - Notification { kind: 'N', value: 'B', error: undefined, hasValue: true } |
| 44 |
* // - Notification { kind: 'E', value: undefined, error: TypeError { message: x.toUpperCase is not a function }, hasValue: false } |
| 45 |
* ``` |
| 46 |
* |
| 47 |
* @see {@link Notification} |
| 48 |
* @see {@link dematerialize} |
| 49 |
* |
| 50 |
* @return A function that returns an Observable that emits |
| 51 |
* {@link Notification} objects that wrap the original emissions from the |
| 52 |
* source Observable with metadata. |
| 53 |
*/ |
| 54 |
export function materialize<T>(): OperatorFunction<T, Notification<T> & ObservableNotification<T>> { |
| 55 |
return operate((source, subscriber) => { |
| 56 |
source.subscribe( |
| 57 |
createOperatorSubscriber( |
| 58 |
subscriber, |
| 59 |
(value) => { |
| 60 |
subscriber.next(Notification.createNext(value)); |
| 61 |
}, |
| 62 |
() => { |
| 63 |
subscriber.next(Notification.createComplete()); |
| 64 |
subscriber.complete(); |
| 65 |
}, |
| 66 |
(err) => { |
| 67 |
subscriber.next(Notification.createError(err)); |
| 68 |
subscriber.complete(); |
| 69 |
} |
| 70 |
) |
| 71 |
); |
| 72 |
}); |
| 73 |
} |