I'm writing a timer stream using Date.now() and I'm having a problem understanding a detail.
When I write the stream using a switchMap, it works fine, and the getTestStartTime$()
is called after the start$ starts emitting events.
let start$ = Observable.fromEvent(document.querySelector('#start'), 'click');
let stop$ = Observable.fromEvent(document.querySelector('#stop'), 'click');
let getTestStartTime$ = () => Observable.of(Date.now())
.delay(sampleTime)
.repeat()
.takeWhile(val => true);
let time$ = start$
.switchMap(event => getTestStartTime$())
.map(startTime => Date.now() - startTime)
.map(diff => diff / 1000)
.takeUntil(stop$)
.repeat();
But when replacing switchMap with switchMapTo it seems the function is called before the start$ is firing. I can see this because Date.now() is called too early (it has the same time as the time of pageload).
let time$ = start$
.switchMapTo(getTestStartTime$()) // this calls getTestStartTime$ too early
.map(startTime => Date.now() - startTime)
.map(diff => diff / 1000)
.takeUntil(stop$)
.repeat();
Thanks.