Last active
September 29, 2020 05:21
-
-
Save crunchie84/45728a2314fa953cf474e30f355161cf to your computer and use it in GitHub Desktop.
RxJs bitcoin stock ticker example
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
Rx.Observable.timer(0, 10 * 1000) | |
.switchMap(_ => fetch('https://blockchain.info/ticker?cors=true') | |
.then(res => res.json()) | |
.then(parsed => parsed.EUR.buy) | |
) | |
.distinctUntilChanged() | |
.scan((acc, curr) => ({ | |
delta: Math.round((acc.value - curr || 0) * 100) / 100, | |
value: curr | |
}), {}) | |
.subscribe(tick => console.log(`1 BTC = ${tick.value} EUR (Δ ${tick.delta > 0 ? '+' : ''}${tick.delta})`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment