I'm trying to make an Observable that, on a button click, emits an array of PAGE_SIZE (the first click should emit [0, 1, 2, 3, 4]), with the caveat that after the button click an interval will begin emitting more numbers that are concatenated to the original (i.e. after the user clicks the interval should emit [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], then [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], etc.).
The following almost does what I want, but I need the whole process to start over when the user clicks the button again.
Any ideas?
const PAGE_SIZE = 5;
let currentPage = 0;
const buttonEl = document.getElementsByTagName('button')[0];
const refreshSource$ = new rxjs.Subject().pipe(rxjs.operators.concatMap(() => rxjs.interval(5000)));
const clickSource$ = new rxjs.fromEvent(buttonEl, 'click').pipe(rxjs.operators.tap(() => {
refreshSource$.next();
refreshSource$.complete();
}));
const clips$ = rxjs.merge(clickSource$, refreshSource$).pipe(rxjs.operators.mergeMap(value => {
if (value.type === undefined)
return makeObservable(value + 1);
else
return makeObservable(0);
}), rxjs.operators.scan((acc, value) => acc.concat(value)), rxjs.operators.startWith([]));
clips$.subscribe(value => {
console.log('clips$', value);
});
function makeObservable(page) {
return rxjs.of(d3.range(page * PAGE_SIZE, (page + 1) * PAGE_SIZE));
}
clickSource$andrefreshSource$are intended to interact? Also doesn't look likecurrentPageis used at present. - backtickcomplete()in the tap operator might be canceling your interval before it starts. - backtickclickSource$should emit each time the button is pressed, andrefreshSource$should emit only afterclickSource$has emitted.. the results should be concatenated together untilclickSource$emits again (i.e. the user clicks the button and restarts the interval) - lwisemancomplete().. if Iconsole.logthe result of themergeMapit, seemingly, correctly shows a clickevent and then subsequent intervals, starting from 0 - lwisemancurrentPagewas before I realized I could just use the interval value itself.. it should be 0 on button click and increase by 1 on each interval tick - lwiseman