Created
August 24, 2016 16:27
-
-
Save anonymous/9d9f1ac156b4f0a77c8a9ab2dff9dc51 to your computer and use it in GitHub Desktop.
JS Bin mapTo // source https://jsbin.com/zoyotam
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 name="description" content="mapTo"> | |
<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="startBtn">Start</button> | |
<button id="halfBtn">Half</button> | |
<button id="quarterBtn">Quarter</button> | |
<button id="stopBtn">Stop</button> | |
<button id="resetBtn">Reset</button> | |
<div id="counter"></div> | |
<input type="text" id="input"> | |
<script id="jsbin-javascript"> | |
const Observable = Rx.Observable; | |
const startButton = document.querySelector('#startBtn'); | |
const stopButton = document.querySelector('#stopBtn'); | |
const resetButton = document.querySelector('#resetBtn'); | |
const quarterButton = document.querySelector('#quarterBtn'); | |
const halfButton = document.querySelector('#halfBtn'); | |
const counter = document.querySelector('#counter'); | |
const start$ = Observable.fromEvent(startButton, 'click'); | |
const startQuarter$ = Observable.fromEvent(quarterButton, 'click'); | |
const startHalf$ = Observable.fromEvent(halfButton, 'click'); | |
const stop$ = Observable.fromEvent(stopButton, 'click'); | |
const reset$ = Observable.fromEvent(resetButton, 'click'); | |
const interval$ = Observable.interval(1000); | |
const intervalThatStops$ = interval$.takeUntil(stop$); | |
const data = {count: 0}; | |
const inc = (acc) => ({count: acc.count + 1}); | |
const resetData = (acc) => data; | |
const starters$ = Observable.merge( | |
start$.mapTo(1000), | |
startHalf$.mapTo(500), | |
startQuarter$.mapTo(250) | |
); | |
// take an observable and then switch to other observable | |
const timer$ = starters$ | |
.switchMap((time) => { | |
return Observable.merge( | |
Observable.interval(time).takeUntil(stop$).mapTo(inc), | |
reset$.mapTo(resetData) | |
) | |
}) | |
.startWith(data) // init scan start value | |
.scan((acc, curr) => { // scan is used to gather data | |
return curr(acc); | |
}) | |
// input | |
const inputField = document.querySelector('#input'); | |
const input$ = Observable.fromEvent(inputField, 'input').map((e) => e.target.value).startWith(''); | |
// Two streams often need to work together to produce the values you’ll need. | |
// Important! combineLatest starts to emit values when both streams of data has a value | |
// That's why I add to input$ startWith() operator. Without it combineLatest emits values | |
// only when you start typing. | |
Observable.combineLatest( | |
timer$, | |
input$, | |
(timer, input) => ({count: timer.count, text: input}) | |
).subscribe((x) => console.log(x)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const Observable = Rx.Observable; | |
const startButton = document.querySelector('#startBtn'); | |
const stopButton = document.querySelector('#stopBtn'); | |
const resetButton = document.querySelector('#resetBtn'); | |
const quarterButton = document.querySelector('#quarterBtn'); | |
const halfButton = document.querySelector('#halfBtn'); | |
const counter = document.querySelector('#counter'); | |
const start$ = Observable.fromEvent(startButton, 'click'); | |
const startQuarter$ = Observable.fromEvent(quarterButton, 'click'); | |
const startHalf$ = Observable.fromEvent(halfButton, 'click'); | |
const stop$ = Observable.fromEvent(stopButton, 'click'); | |
const reset$ = Observable.fromEvent(resetButton, 'click'); | |
const interval$ = Observable.interval(1000); | |
const intervalThatStops$ = interval$.takeUntil(stop$); | |
const data = {count: 0}; | |
const inc = (acc) => ({count: acc.count + 1}); | |
const resetData = (acc) => data; | |
const starters$ = Observable.merge( | |
start$.mapTo(1000), | |
startHalf$.mapTo(500), | |
startQuarter$.mapTo(250) | |
); | |
// take an observable and then switch to other observable | |
const timer$ = starters$ | |
.switchMap((time) => { | |
return Observable.merge( | |
Observable.interval(time).takeUntil(stop$).mapTo(inc), | |
reset$.mapTo(resetData) | |
) | |
}) | |
.startWith(data) // init scan start value | |
.scan((acc, curr) => { // scan is used to gather data | |
return curr(acc); | |
}) | |
// input | |
const inputField = document.querySelector('#input'); | |
const input$ = Observable.fromEvent(inputField, 'input').map((e) => e.target.value).startWith(''); | |
// Two streams often need to work together to produce the values you’ll need. | |
// Important! combineLatest starts to emit values when both streams of data has a value | |
// That's why I add to input$ startWith() operator. Without it combineLatest emits values | |
// only when you start typing. | |
Observable.combineLatest( | |
timer$, | |
input$, | |
(timer, input) => ({count: timer.count, text: input}) | |
).subscribe((x) => console.log(x)); | |
</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
const Observable = Rx.Observable; | |
const startButton = document.querySelector('#startBtn'); | |
const stopButton = document.querySelector('#stopBtn'); | |
const resetButton = document.querySelector('#resetBtn'); | |
const quarterButton = document.querySelector('#quarterBtn'); | |
const halfButton = document.querySelector('#halfBtn'); | |
const counter = document.querySelector('#counter'); | |
const start$ = Observable.fromEvent(startButton, 'click'); | |
const startQuarter$ = Observable.fromEvent(quarterButton, 'click'); | |
const startHalf$ = Observable.fromEvent(halfButton, 'click'); | |
const stop$ = Observable.fromEvent(stopButton, 'click'); | |
const reset$ = Observable.fromEvent(resetButton, 'click'); | |
const interval$ = Observable.interval(1000); | |
const intervalThatStops$ = interval$.takeUntil(stop$); | |
const data = {count: 0}; | |
const inc = (acc) => ({count: acc.count + 1}); | |
const resetData = (acc) => data; | |
const starters$ = Observable.merge( | |
start$.mapTo(1000), | |
startHalf$.mapTo(500), | |
startQuarter$.mapTo(250) | |
); | |
// take an observable and then switch to other observable | |
const timer$ = starters$ | |
.switchMap((time) => { | |
return Observable.merge( | |
Observable.interval(time).takeUntil(stop$).mapTo(inc), | |
reset$.mapTo(resetData) | |
) | |
}) | |
.startWith(data) // init scan start value | |
.scan((acc, curr) => { // scan is used to gather data | |
return curr(acc); | |
}) | |
// input | |
const inputField = document.querySelector('#input'); | |
const input$ = Observable.fromEvent(inputField, 'input').map((e) => e.target.value).startWith(''); | |
// Two streams often need to work together to produce the values you’ll need. | |
// Important! combineLatest starts to emit values when both streams of data has a value | |
// That's why I add to input$ startWith() operator. Without it combineLatest emits values | |
// only when you start typing. | |
Observable.combineLatest( | |
timer$, | |
input$, | |
(timer, input) => ({count: timer.count, text: input}) | |
).subscribe((x) => console.log(x)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment