Last active
March 1, 2016 13:18
-
-
Save mzahor/49e211ed0a9cf03edb27 to your computer and use it in GitHub Desktop.
Controlling one stream with another stream in RxJS
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
<!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> |
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
'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