Skip to content

Commit

Permalink
feat(dematerialize): add higher-ordeer lettable version of dematerialize
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jul 13, 2017
1 parent a780bf2 commit b5948f9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 25 deletions.
29 changes: 4 additions & 25 deletions src/operator/dematerialize.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 { dematerialize as higherOrder } from '../operators';

/**
* Converts an Observable of {@link Notification} objects into the emissions
Expand Down Expand Up @@ -43,27 +43,6 @@ import { Notification } from '../Notification';
* @method dematerialize
* @owner Observable
*/
export function dematerialize<T>(this: Observable<T>): Observable<any> {
return this.lift(new DeMaterializeOperator());
}

class DeMaterializeOperator<T extends Notification<any>, R> implements Operator<T, R> {
call(subscriber: Subscriber<any>, source: any): any {
return source.subscribe(new DeMaterializeSubscriber(subscriber));
}
}

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

protected _next(value: T) {
value.observe(this.destination);
}
export function dematerialize<T>(this: Observable<Notification<T>>): Observable<T> {
return higherOrder()(this);
}
72 changes: 72 additions & 0 deletions src/operators/dematerialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Operator } from '../Operator';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { Notification } from '../Notification';
import { OperatorFunction } from '../interfaces';

/**
* Converts an Observable of {@link Notification} objects into the emissions
* that they represent.
*
* <span class="informal">Unwraps {@link Notification} objects as actual `next`,
* `error` and `complete` emissions. The opposite of {@link materialize}.</span>
*
* <img src="./img/dematerialize.png" width="100%">
*
* `dematerialize` is assumed to operate an Observable that only emits
* {@link Notification} objects as `next` emissions, and does not emit any
* `error`. Such Observable is the output of a `materialize` operation. Those
* notifications are then unwrapped using the metadata they contain, and emitted
* as `next`, `error`, and `complete` on the output Observable.
*
* Use this operator in conjunction with {@link materialize}.
*
* @example <caption>Convert an Observable of Notifications to an actual Observable</caption>
* var notifA = new Rx.Notification('N', 'A');
* var notifB = new Rx.Notification('N', 'B');
* var notifE = new Rx.Notification('E', void 0,
* new TypeError('x.toUpperCase is not a function')
* );
* var materialized = Rx.Observable.of(notifA, notifB, notifE);
* var upperCase = materialized.dematerialize();
* upperCase.subscribe(x => console.log(x), e => console.error(e));
*
* // Results in:
* // A
* // B
* // TypeError: x.toUpperCase is not a function
*
* @see {@link Notification}
* @see {@link materialize}
*
* @return {Observable} An Observable that emits items and notifications
* embedded in Notification objects emitted by the source Observable.
* @method dematerialize
* @owner Observable
*/
export function dematerialize<T>(): OperatorFunction<Notification<T>, T> {
return function dematerializeOperatorFunction(source: Observable<Notification<T>>) {
return source.lift(new DeMaterializeOperator());
};
}

class DeMaterializeOperator<T extends Notification<any>, R> implements Operator<T, R> {
call(subscriber: Subscriber<any>, source: any): any {
return source.subscribe(new DeMaterializeSubscriber(subscriber));
}
}

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

protected _next(value: T) {
value.observe(this.destination);
}
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { concat } from './concat';
export { concatAll } from './concatAll';
export { concatMap } from './concatMap';
export { defaultIfEmpty } from './defaultIfEmpty';
export { dematerialize } from './dematerialize';
export { filter } from './filter';
export { ignoreElements } from './ignoreElements';
export { map } from './map';
Expand Down

0 comments on commit b5948f9

Please sign in to comment.