Created
July 7, 2017 03:57
-
-
Save sguzman/eec184efcf84489270cb23b27f3e815b to your computer and use it in GitHub Desktop.
A pretty Rxjs counter
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="x-ua-compatible" content="ie=edge" /> | |
<meta name="author" content="Salvador Guzman" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<title>RxJS Counter</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.2/Rx.min.js"></script> | |
</head> | |
<body> | |
<h1>RxJS Counter</h1> | |
<h2 id="counter">0</h2> | |
<button id="button1" type="button">Button 1</button> | |
<button id="button2" type="button">Button 2</button> | |
<script> | |
const butt1 = document.getElementById('button1'); | |
const butt2 = document.getElementById('button2'); | |
const count = document.getElementById('counter'); | |
const butt1$ = Rx.Observable.fromEvent(butt1, 'click').map(s => 1); | |
const butt2$ = Rx.Observable.fromEvent(butt2, 'click').map(s => -1); | |
butt1$.merge(butt2$) | |
.scan((s,t) => s + t, 0) | |
.subscribe(s => { | |
count.innerText = s; | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment