JS Bin// source https://jsbin.com/jewezex RxJS - Display initial data with StartWith
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://unpkg.com/@reactivex/rxjs@5.0.0-beta.7/dist/global/Rx.umd.js"></script>
</head>
<body>
<button id="start">Start</button>
<button id="stop">Stop</button>
<script id="jsbin-javascript">
'use strict';
var start = document.querySelector('#start');
var stop = document.querySelector('#stop');
var start$ = Rx.Observable.fromEvent(start, 'click');
var interval$ = Rx.Observable.interval(1000);
var stop$ = Rx.Observable.fromEvent(stop, 'click');
var intervalThatStops$ = interval$.takeUntil(stop$);
var data = { count: 5 };
start$.switchMapTo(intervalThatStops$).startWith(data).scan(function (acc) {
return { count: acc.count + 1 };
}).subscribe(function (x) {
return console.log(x);
});
</script>
<script id="jsbin-source-html" type="text/html">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://unpkg.com/@reactivex/rxjs@5.0.0-beta.7/dist/global/Rx.umd.js"><\/script>
</head>
<body>
<button id="start">Start</button>
<button id="stop">Stop</button>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">const start = document.querySelector('#start');
const stop = document.querySelector('#stop');
const start$ = Rx.Observable.fromEvent(start, 'click');
const interval$ = Rx.Observable.interval(1000);
const stop$ = Rx.Observable.fromEvent(stop, 'click');
const intervalThatStops$ = interval$
.takeUntil(stop$);
const data = { count: 5 };
start$
.switchMapTo(intervalThatStops$)
.startWith(data)
.scan((acc) => {
return {count: acc.count + 1}
})
.subscribe((x) => console.log(x));
</script></body>
</html>
'use strict';
var start = document.querySelector('#start');
var stop = document.querySelector('#stop');
var start$ = Rx.Observable.fromEvent(start, 'click');
var interval$ = Rx.Observable.interval(1000);
var stop$ = Rx.Observable.fromEvent(stop, 'click');
var intervalThatStops$ = interval$.takeUntil(stop$);
var data = { count: 5 };
start$.switchMapTo(intervalThatStops$).startWith(data).scan(function (acc) {
return { count: acc.count + 1 };
}).subscribe(function (x) {
return console.log(x);
});