Skip to content

Instantly share code, notes, and snippets.

@mzahor
Last active March 1, 2016 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzahor/49e211ed0a9cf03edb27 to your computer and use it in GitHub Desktop.
Save mzahor/49e211ed0a9cf03edb27 to your computer and use it in GitHub Desktop.
Controlling one stream with another stream in RxJS
<!DOCTYPE html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script>
</head>
<body>
<label><input id="chb" type="checkbox">Run</label>
<div id="output"></div>
</body>
'use strict';
var output = document.querySelector('#output');
var chb = document.querySelector('#chb');
var source = Rx.Observable.timer(0, 100).map(function () {
return '.';
});
var checked = Rx.Observable.fromEvent(chb, 'change').map(function (x) {
return x.target.checked;
});
checked.filter(function (x) {
return !!x;
}).flatMapLatest(function () {
return source.takeUntil(checked);
}).forEach(function (x) {
return output.innerHTML += x;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment