-
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.
closes #209
- Loading branch information
Showing
4 changed files
with
102 additions
and
1 deletion.
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,27 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.withLatestFrom()', function () { | ||
it('should merge the emitted value with the latest values of the other observables', function (done) { | ||
var a = Observable.of('a'); | ||
var b = Observable.of('b', 'c'); | ||
|
||
Observable.value('d').delay(100) | ||
.withLatestFrom(a, b, function (x, a, b) { return [x, a, b]; }) | ||
.subscribe(function (x) { | ||
expect(x).toEqual(['d', 'a', 'c']); | ||
}, null, done); | ||
}); | ||
|
||
it('should emit nothing if the other observables never emit', function (done) { | ||
var a = Observable.of('a'); | ||
var b = Observable.never(); | ||
|
||
Observable.value('d').delay(100) | ||
.withLatestFrom(a, b, function (x, a, b) { return [x, a, b]; }) | ||
.subscribe(function (x) { | ||
expect('this was called').toBe(false); | ||
}, 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,72 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
import Observable from '../Observable'; | ||
|
||
import tryCatch from '../util/tryCatch'; | ||
import {errorObject} from '../util/errorObject'; | ||
|
||
export default function withLatestFrom<R>(...args: (Observable<any>|((...values: any[]) => Observable<R>))[]): Observable<R> { | ||
const project = <((...values: any[]) => Observable<R>)>args.pop(); | ||
const observables = <Observable<any>[]>args; | ||
return this.lift(new WithLatestFromOperator(observables, project)); | ||
} | ||
|
||
export class WithLatestFromOperator<T, R> implements Operator<T, R> { | ||
constructor(private observables: Observable<any>[], private project: (...values: any[]) => Observable<R>) { | ||
} | ||
|
||
call(observer: Observer<R>): Observer<T> { | ||
return new WithLatestFromSubscriber<T, R>(observer, this.observables, this.project); | ||
} | ||
} | ||
|
||
export class WithLatestFromSubscriber<T, R> extends Subscriber<T> { | ||
private values: any[]; | ||
private toSet: number; | ||
|
||
constructor(destination: Observer<T>, private observables: Observable<any>[], private project: (...values: any[]) => Observable<R>) { | ||
super(destination); | ||
const len = observables.length; | ||
this.values = new Array(len); | ||
this.toSet = len; | ||
for (let i = 0; i < len; i++) { | ||
this.add(observables[i].subscribe(new WithLatestInnerSubscriber(this, i))) | ||
} | ||
} | ||
|
||
notifyValue(index, value) { | ||
this.values[index] = value; | ||
this.toSet--; | ||
} | ||
|
||
_next(value: T) { | ||
if (this.toSet === 0) { | ||
const values = this.values; | ||
let result = tryCatch(this.project)([value, ...values]); | ||
if (result === errorObject) { | ||
this.destination.error(result.e); | ||
} else { | ||
this.destination.next(result); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export class WithLatestInnerSubscriber<T, R> extends Subscriber<T> { | ||
constructor(private parent: WithLatestFromSubscriber<T, R>, private valueIndex: number) { | ||
super(null) | ||
} | ||
|
||
_next(value: T) { | ||
this.parent.notifyValue(this.valueIndex, value); | ||
} | ||
|
||
_error(err: any) { | ||
this.parent.error(err); | ||
} | ||
|
||
_complete() { | ||
// noop | ||
} | ||
} |