-
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(observable): add Observable.all (forkJoin)
- Loading branch information
Showing
4 changed files
with
74 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,14 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.forkJoin', function () { | ||
it('should join the last values of the provided observables into an array', function(done) { | ||
Observable.forkJoin(Observable.of(1, 2, 3, 'a'), | ||
Observable.of('b'), | ||
Observable.of(1, 2, 3, 4, 'c')) | ||
.subscribe(function (x) { | ||
expect(x).toEqual(['a', 'b', 'c']); | ||
}, 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,56 @@ | ||
import Observable from '../Observable'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
|
||
export default class ForkJoinObservable<T> extends Observable<T> { | ||
constructor(private observables: Observable<any>[]) { | ||
super(); | ||
} | ||
|
||
static create<R>(...observables: Observable<any>[]): Observable<R> { | ||
return new ForkJoinObservable(observables); | ||
} | ||
|
||
_subscribe(subscriber: Observer<any>) { | ||
const observables = this.observables; | ||
const len = observables.length; | ||
let context = { complete: 0, total: len, values: emptyArray(len) }; | ||
for (let i = 0; i < len; i++) { | ||
observables[i].subscribe(new AllSubscriber(subscriber, this, i, context)) | ||
} | ||
} | ||
} | ||
|
||
class AllSubscriber<T> extends Subscriber<T> { | ||
private _value: T; | ||
|
||
constructor(destination: Observer<T>, private parent: ForkJoinObservable<T>, private index: number, | ||
private context: { complete: number, total: number, values: any[] }) { | ||
super(destination); | ||
} | ||
|
||
_next(value: T) { | ||
this._value = value; | ||
} | ||
|
||
_complete() { | ||
const context = this.context; | ||
context.values[this.index] = this._value; | ||
if (context.values.every(hasValue)) { | ||
this.destination.next(context.values); | ||
this.destination.complete(); | ||
} | ||
} | ||
} | ||
|
||
function hasValue(x) { | ||
return x !== null; | ||
} | ||
|
||
function emptyArray(len: number): any[] { | ||
var arr = []; | ||
for (let i = 0; i < len; i++) { | ||
arr.push(null); | ||
} | ||
return arr; | ||
} |