Skip to content

Commit ce42477

Browse files
committed
feat(materialize): add higher-order lettable materialize operator
1 parent b5948f9 commit ce42477

File tree

3 files changed

+92
-36
lines changed

3 files changed

+92
-36
lines changed

src/operator/materialize.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Operator } from '../Operator';
1+
22
import { Observable } from '../Observable';
3-
import { Subscriber } from '../Subscriber';
43
import { Notification } from '../Notification';
4+
import { materialize as higherOrder } from '../operators';
55

66
/**
77
* Represents all of the notifications from the source Observable as `next`
@@ -48,38 +48,5 @@ import { Notification } from '../Notification';
4848
* @owner Observable
4949
*/
5050
export function materialize<T>(this: Observable<T>): Observable<Notification<T>> {
51-
return this.lift(new MaterializeOperator());
52-
}
53-
54-
class MaterializeOperator<T> implements Operator<T, Notification<T>> {
55-
call(subscriber: Subscriber<Notification<T>>, source: any): any {
56-
return source.subscribe(new MaterializeSubscriber(subscriber));
57-
}
58-
}
59-
60-
/**
61-
* We need this JSDoc comment for affecting ESDoc.
62-
* @ignore
63-
* @extends {Ignored}
64-
*/
65-
class MaterializeSubscriber<T> extends Subscriber<T> {
66-
constructor(destination: Subscriber<Notification<T>>) {
67-
super(destination);
68-
}
69-
70-
protected _next(value: T) {
71-
this.destination.next(Notification.createNext(value));
72-
}
73-
74-
protected _error(err: any) {
75-
const destination = this.destination;
76-
destination.next(Notification.createError(err));
77-
destination.complete();
78-
}
79-
80-
protected _complete() {
81-
const destination = this.destination;
82-
destination.next(Notification.createComplete());
83-
destination.complete();
84-
}
51+
return higherOrder()(this);
8552
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export { dematerialize } from './dematerialize';
99
export { filter } from './filter';
1010
export { ignoreElements } from './ignoreElements';
1111
export { map } from './map';
12+
export { materialize } from './materialize';
1213
export { max } from './max';
1314
export { mergeAll } from './mergeAll';
1415
export { mergeMap } from './mergeMap';

src/operators/materialize.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { Operator } from '../Operator';
2+
import { Observable } from '../Observable';
3+
import { Subscriber } from '../Subscriber';
4+
import { Notification } from '../Notification';
5+
import { OperatorFunction } from '../interfaces';
6+
7+
/**
8+
* Represents all of the notifications from the source Observable as `next`
9+
* emissions marked with their original types within {@link Notification}
10+
* objects.
11+
*
12+
* <span class="informal">Wraps `next`, `error` and `complete` emissions in
13+
* {@link Notification} objects, emitted as `next` on the output Observable.
14+
* </span>
15+
*
16+
* <img src="./img/materialize.png" width="100%">
17+
*
18+
* `materialize` returns an Observable that emits a `next` notification for each
19+
* `next`, `error`, or `complete` emission of the source Observable. When the
20+
* source Observable emits `complete`, the output Observable will emit `next` as
21+
* a Notification of type "complete", and then it will emit `complete` as well.
22+
* When the source Observable emits `error`, the output will emit `next` as a
23+
* Notification of type "error", and then `complete`.
24+
*
25+
* This operator is useful for producing metadata of the source Observable, to
26+
* be consumed as `next` emissions. Use it in conjunction with
27+
* {@link dematerialize}.
28+
*
29+
* @example <caption>Convert a faulty Observable to an Observable of Notifications</caption>
30+
* var letters = Rx.Observable.of('a', 'b', 13, 'd');
31+
* var upperCase = letters.map(x => x.toUpperCase());
32+
* var materialized = upperCase.materialize();
33+
* materialized.subscribe(x => console.log(x));
34+
*
35+
* // Results in the following:
36+
* // - Notification {kind: "N", value: "A", error: undefined, hasValue: true}
37+
* // - Notification {kind: "N", value: "B", error: undefined, hasValue: true}
38+
* // - Notification {kind: "E", value: undefined, error: TypeError:
39+
* // x.toUpperCase is not a function at MapSubscriber.letters.map.x
40+
* // [as project] (http://1…, hasValue: false}
41+
*
42+
* @see {@link Notification}
43+
* @see {@link dematerialize}
44+
*
45+
* @return {Observable<Notification<T>>} An Observable that emits
46+
* {@link Notification} objects that wrap the original emissions from the source
47+
* Observable with metadata.
48+
* @method materialize
49+
* @owner Observable
50+
*/
51+
export function materialize<T>(): OperatorFunction<T, Notification<T>> {
52+
return function materializeOperatorFunction(source: Observable<T>) {
53+
return source.lift(new MaterializeOperator());
54+
};
55+
}
56+
57+
class MaterializeOperator<T> implements Operator<T, Notification<T>> {
58+
call(subscriber: Subscriber<Notification<T>>, source: any): any {
59+
return source.subscribe(new MaterializeSubscriber(subscriber));
60+
}
61+
}
62+
63+
/**
64+
* We need this JSDoc comment for affecting ESDoc.
65+
* @ignore
66+
* @extends {Ignored}
67+
*/
68+
class MaterializeSubscriber<T> extends Subscriber<T> {
69+
constructor(destination: Subscriber<Notification<T>>) {
70+
super(destination);
71+
}
72+
73+
protected _next(value: T) {
74+
this.destination.next(Notification.createNext(value));
75+
}
76+
77+
protected _error(err: any) {
78+
const destination = this.destination;
79+
destination.next(Notification.createError(err));
80+
destination.complete();
81+
}
82+
83+
protected _complete() {
84+
const destination = this.destination;
85+
destination.next(Notification.createComplete());
86+
destination.complete();
87+
}
88+
}

0 commit comments

Comments
 (0)