-
-
Save zhenyanghua/a63209bb67b3d3b714798c1a573f8c98 to your computer and use it in GitHub Desktop.
Toggle Subscription
This file contains hidden or 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 hidden or 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/[email protected]/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 hidden or 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