Skip to content

Instantly share code, notes, and snippets.

@Olgagr
Forked from anonymous/index.html
Created August 25, 2016 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Olgagr/0c95e64078fd40ebdb154b63281dd735 to your computer and use it in GitHub Desktop.
Save Olgagr/0c95e64078fd40ebdb154b63281dd735 to your computer and use it in GitHub Desktop.
reduce/filter/do
<!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/rxjs@5.0.0-beta.1/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('');
// When a stream has completed, you often need to evaluate everything
// that has happened while the stream was running. This is when reduce comes in. Reduce causes
// that subscribe is called only once, after the stream is completed.
//
// do => this is very helpful if we want to debug what is going on in the stream. This
// is called outside the stream (no influence on the stream), but it gets current value
// in the stream.
Observable.combineLatest(
timer$,
input$,
(timer, input) => ({count: timer.count, text: input})
)
.do((x) => console.log(x))
.takeWhile((data) => data.count <= 3)
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + 1, 0)
.subscribe(
x => console.log(x),
err => console.log(err),
() => console.log('complete!')
);
</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('');
// When a stream has completed, you often need to evaluate everything
// that has happened while the stream was running. This is when reduce comes in. Reduce causes
// that subscribe is called only once, after the stream is completed.
//
// do => this is very helpful if we want to debug what is going on in the stream. This
// is called outside the stream (no influence on the stream), but it gets current value
// in the stream.
Observable.combineLatest(
timer$,
input$,
(timer, input) => ({count: timer.count, text: input})
)
.do((x) => console.log(x))
.takeWhile((data) => data.count <= 3)
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + 1, 0)
.subscribe(
x => console.log(x),
err => console.log(err),
() => console.log('complete!')
);
</script></body>
</html>
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('');
// When a stream has completed, you often need to evaluate everything
// that has happened while the stream was running. This is when reduce comes in. Reduce causes
// that subscribe is called only once, after the stream is completed.
//
// do => this is very helpful if we want to debug what is going on in the stream. This
// is called outside the stream (no influence on the stream), but it gets current value
// in the stream.
Observable.combineLatest(
timer$,
input$,
(timer, input) => ({count: timer.count, text: input})
)
.do((x) => console.log(x))
.takeWhile((data) => data.count <= 3)
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + 1, 0)
.subscribe(
x => console.log(x),
err => console.log(err),
() => console.log('complete!')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment