Skip to content

Instantly share code, notes, and snippets.

@swazza
Created March 6, 2017 04:47
Show Gist options
  • Save swazza/2aad7327ed66ff305bc81d1a6b6aaeb5 to your computer and use it in GitHub Desktop.
Save swazza/2aad7327ed66ff305bc81d1a6b6aaeb5 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/bovexi
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="mount">
<button id="decrement">-</button>
<label id="count"></label>
<button id="increment">+</button>
</div>
<script src="https://unpkg.com/@reactivex/rxjs@5.0.3/dist/global/Rx.js"></script>
<script id="jsbin-javascript">
document.addEventListener('DOMContentLoaded', () => {
let increment = document.querySelector('#increment'),
decrement = document.querySelector('#decrement'),
count = document.querySelector('#count');
let increment$ = Rx.Observable
.fromEvent(increment, 'click')
.map(e => 1);
let decrement$ = Rx.Observable
.fromEvent(decrement, 'click')
.map(e => -1);
let count$ = Rx.Observable
.empty()
.defaultIfEmpty(0)
.merge(increment$, decrement$)
.scan((acc, curr) => acc + curr, 0)
count$.subscribe(data => {
count.textContent = data
})
})
</script>
<script id="jsbin-source-javascript" type="text/javascript">document.addEventListener('DOMContentLoaded', () => {
let increment = document.querySelector('#increment'),
decrement = document.querySelector('#decrement'),
count = document.querySelector('#count');
let increment$ = Rx.Observable
.fromEvent(increment, 'click')
.map(e => 1);
let decrement$ = Rx.Observable
.fromEvent(decrement, 'click')
.map(e => -1);
let count$ = Rx.Observable
.empty()
.defaultIfEmpty(0)
.merge(increment$, decrement$)
.scan((acc, curr) => acc + curr, 0)
count$.subscribe(data => {
count.textContent = data
})
})
</script></body>
</html>
document.addEventListener('DOMContentLoaded', () => {
let increment = document.querySelector('#increment'),
decrement = document.querySelector('#decrement'),
count = document.querySelector('#count');
let increment$ = Rx.Observable
.fromEvent(increment, 'click')
.map(e => 1);
let decrement$ = Rx.Observable
.fromEvent(decrement, 'click')
.map(e => -1);
let count$ = Rx.Observable
.empty()
.defaultIfEmpty(0)
.merge(increment$, decrement$)
.scan((acc, curr) => acc + curr, 0)
count$.subscribe(data => {
count.textContent = data
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment