Created
March 22, 2016 16:55
-
-
Save johnlindquist/1185b7701932b028401b to your computer and use it in GitHub Desktop.
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
const Observable = Rx.Observable; | |
const toggleButton = document.querySelector('#toggle'); | |
const toggleClick$ = Observable.fromEvent(toggleButton, 'click'); | |
const interval$ = Observable.interval(1000); | |
const toggle$ = toggleClick$ | |
.startWith(false) | |
.scan((acc, curr)=> !acc); //toggles true/false | |
toggle$ | |
//project the true/false each interval "tick" | |
.combineLatest(interval$, bool => bool) | |
//only push when true | |
.filter(bool=> bool) | |
.startWith(0) | |
.scan(acc => acc + 1) | |
.subscribe(console.log.bind(console)); | |
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> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.3/dist/global/Rx.umd.js"></script> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<button id="toggle">Toggle</button> | |
<script src="./app.js"></script> | |
</body> | |
</html> |
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
/* todo: add styles */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment