-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): Add expand operator.
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.expand()', function () { | ||
it('should map and recursively flatten', function (done) { | ||
var expected = [1, 2, 3, 4, 5]; | ||
Observable.of(0).expand(function (x) { | ||
if (x > 4) { | ||
return Observable.empty(); | ||
} | ||
return Observable.of(x + 1); | ||
}) | ||
.subscribe(function (x) { | ||
expect(x).toBe(expected.shift()); | ||
}, null, done); | ||
}); | ||
it('should map and recursively flatten with ScalarObservables', function (done) { | ||
var expected = [1, 2, 3, 4, 5]; | ||
Observable.return(0).expand(function (x) { | ||
if (x > 4) { | ||
return Observable.empty(); | ||
} | ||
return Observable.return(x + 1); | ||
}) | ||
.subscribe(function (x) { | ||
expect(x).toBe(expected.shift()); | ||
}, null, done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Observable from '../Observable'; | ||
import Subscriber from '../Subscriber'; | ||
|
||
import {MergeSubscriber, MergeInnerSubscriber} from './merge'; | ||
import EmptyObservable from '../observables/EmptyObservable'; | ||
import ScalarObservable from '../observables/ScalarObservable'; | ||
|
||
import tryCatch from '../util/tryCatch'; | ||
import {errorObject} from '../util/errorObject'; | ||
|
||
export default function expand<T>(project: (x: T, ix: number) => Observable<any>): Observable<any> { | ||
return this.lift(new ExpandOperator(project)); | ||
} | ||
|
||
export class ExpandOperator<T, R> extends Operator<T, R> { | ||
|
||
project: (x: T, ix: number) => Observable<any>; | ||
|
||
constructor(project: (x: T, ix: number) => Observable<any>) { | ||
super(); | ||
this.project = project; | ||
} | ||
|
||
call(observer: Observer<R>): Observer<T> { | ||
return new ExpandSubscriber(observer, this.project); | ||
} | ||
} | ||
|
||
export class ExpandSubscriber<T, R> extends MergeSubscriber<T, R> { | ||
|
||
project: (x: T, ix: number) => Observable<any>; | ||
|
||
constructor(destination: Observer<R>, | ||
project: (x: T, ix: number) => Observable<any>) { | ||
super(destination, Number.POSITIVE_INFINITY); | ||
this.project = project; | ||
} | ||
|
||
_project(value, index) { | ||
const observable = tryCatch(this.project).call(this, value, index); | ||
if (observable === errorObject) { | ||
this.error(errorObject.e); | ||
return null; | ||
} | ||
return observable; | ||
} | ||
|
||
_subscribeInner(observable, value, index) { | ||
if(observable instanceof ScalarObservable) { | ||
this.destination.next((<ScalarObservable<T>> observable).value); | ||
this._innerComplete(); | ||
this._next((<ScalarObservable<T>> observable).value); | ||
} else if(observable instanceof EmptyObservable) { | ||
this._innerComplete(); | ||
} else { | ||
return observable.subscribe(new ExpandInnerSubscriber(this)); | ||
} | ||
} | ||
} | ||
|
||
export class ExpandInnerSubscriber<T, R> extends MergeInnerSubscriber<T, R> { | ||
constructor(parent: ExpandSubscriber<T, R>) { | ||
super(parent); | ||
} | ||
_next(value) { | ||
this.destination.next(value); | ||
this.parent.next(value); | ||
} | ||
} |