Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created March 12, 2012 17:03
Show Gist options
  • Save mattpodwysocki/2023369 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/2023369 to your computer and use it in GitHub Desktop.
RxJS TestBed for forkJoin
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>RxJS TestBed</title>
<script type="text/javascript" src="rx.js"></script>
<script type="text/javascript" src="rx.aggregates.js"></script>
<script type="text/javascript">
window.onload = function () {
Rx.Observable.forkJoin = function (sources) {
return Rx.Observable.fromArray(sources)
.select(function(o, i) {
return o.takeLast(1).select(function(value) { return { i: i, value: value }; });
})
.mergeObservable()
.aggregate({ array: [], count: 0 }, function(results, result) {
results.array[result.i] = result.value;
return {
array: results.array,
count: results.count + 1
};
})
.where(function(results) { return results.count === sources.length; })
.select(function(results) { return results.array; });
};
var obs1 = Rx.Observable.returnValue(1)
, obs2 = Rx.Observable.returnValue(2)
, obs3 = Rx.Observable.returnValue(3);
var obs4 = Rx.Observable.forkJoin([obs1, obs2, obs3]);
obs4.subscribe(function (results) {
for (var i = 0, len = results.length; i < len; i++) {
console.log(results[i]);
}
});
};
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment