Skip to content

Commit

Permalink
feat(materialize): add higher-order lettable materialize operator
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jul 13, 2017
1 parent b5948f9 commit ce42477
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 36 deletions.
39 changes: 3 additions & 36 deletions src/operator/materialize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Operator } from '../Operator';

import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { Notification } from '../Notification';
import { materialize as higherOrder } from '../operators';

/**
* Represents all of the notifications from the source Observable as `next`
Expand Down Expand Up @@ -48,38 +48,5 @@ import { Notification } from '../Notification';
* @owner Observable
*/
export function materialize<T>(this: Observable<T>): Observable<Notification<T>> {
return this.lift(new MaterializeOperator());
}

class MaterializeOperator<T> implements Operator<T, Notification<T>> {
call(subscriber: Subscriber<Notification<T>>, source: any): any {
return source.subscribe(new MaterializeSubscriber(subscriber));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class MaterializeSubscriber<T> extends Subscriber<T> {
constructor(destination: Subscriber<Notification<T>>) {
super(destination);
}

protected _next(value: T) {
this.destination.next(Notification.createNext(value));
}

protected _error(err: any) {
const destination = this.destination;
destination.next(Notification.createError(err));
destination.complete();
}

protected _complete() {
const destination = this.destination;
destination.next(Notification.createComplete());
destination.complete();
}
return higherOrder()(this);
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { dematerialize } from './dematerialize';
export { filter } from './filter';
export { ignoreElements } from './ignoreElements';
export { map } from './map';
export { materialize } from './materialize';
export { max } from './max';
export { mergeAll } from './mergeAll';
export { mergeMap } from './mergeMap';
Expand Down
88 changes: 88 additions & 0 deletions src/operators/materialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Operator } from '../Operator';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { Notification } from '../Notification';
import { OperatorFunction } from '../interfaces';

/**
* Represents all of the notifications from the source Observable as `next`
* emissions marked with their original types within {@link Notification}
* objects.
*
* <span class="informal">Wraps `next`, `error` and `complete` emissions in
* {@link Notification} objects, emitted as `next` on the output Observable.
* </span>
*
* <img src="./img/materialize.png" width="100%">
*
* `materialize` returns an Observable that emits a `next` notification for each
* `next`, `error`, or `complete` emission of the source Observable. When the
* source Observable emits `complete`, the output Observable will emit `next` as
* a Notification of type "complete", and then it will emit `complete` as well.
* When the source Observable emits `error`, the output will emit `next` as a
* Notification of type "error", and then `complete`.
*
* This operator is useful for producing metadata of the source Observable, to
* be consumed as `next` emissions. Use it in conjunction with
* {@link dematerialize}.
*
* @example <caption>Convert a faulty Observable to an Observable of Notifications</caption>
* var letters = Rx.Observable.of('a', 'b', 13, 'd');
* var upperCase = letters.map(x => x.toUpperCase());
* var materialized = upperCase.materialize();
* materialized.subscribe(x => console.log(x));
*
* // Results in the following:
* // - Notification {kind: "N", value: "A", error: undefined, hasValue: true}
* // - Notification {kind: "N", value: "B", error: undefined, hasValue: true}
* // - Notification {kind: "E", value: undefined, error: TypeError:
* // x.toUpperCase is not a function at MapSubscriber.letters.map.x
* // [as project] (http://1…, hasValue: false}
*
* @see {@link Notification}
* @see {@link dematerialize}
*
* @return {Observable<Notification<T>>} An Observable that emits
* {@link Notification} objects that wrap the original emissions from the source
* Observable with metadata.
* @method materialize
* @owner Observable
*/
export function materialize<T>(): OperatorFunction<T, Notification<T>> {
return function materializeOperatorFunction(source: Observable<T>) {
return source.lift(new MaterializeOperator());
};
}

class MaterializeOperator<T> implements Operator<T, Notification<T>> {
call(subscriber: Subscriber<Notification<T>>, source: any): any {
return source.subscribe(new MaterializeSubscriber(subscriber));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class MaterializeSubscriber<T> extends Subscriber<T> {
constructor(destination: Subscriber<Notification<T>>) {
super(destination);
}

protected _next(value: T) {
this.destination.next(Notification.createNext(value));
}

protected _error(err: any) {
const destination = this.destination;
destination.next(Notification.createError(err));
destination.complete();
}

protected _complete() {
const destination = this.destination;
destination.next(Notification.createComplete());
destination.complete();
}
}

0 comments on commit ce42477

Please sign in to comment.