ОБНОВЛЕНО
на основе обсуждения в комментариях
const events$ = timer(0, 6000).pipe(
take(3),
switchMap(x =>
timer(0, 300).pipe(
map(num => `event #${num + 1}`),
take(x > 1 ? 9 : 10)
)
)
);
const source = merge(
events$.pipe(
tap(e => console.log(`%cStream: ${e}`, "color: blue;")),
debounceTime(1000),
tap(x => console.log(`%cdebounceTime captured: ${x}`, "color: red;"))
),
events$.pipe(
throttleTime(1000),
tap(x => console.log(`%cthrottleTime captured: ${x}`, "color: green;"))
),
).pipe(
// we need to avoid duplicates (like case with 9).
// if all events aren't unique you need to use the original solution below.
distinctUntilChanged(), // <-- if all events are unique.
map(x => `Received ${x}!`)
);
source.subscribe(x =>
console.log(
"%c" + x,
"background: purple; color: white; padding: 3px 5px; border-radius: 3px;"
)
);
ОРИГИНАЛ
Надеюсь, это то, что вы хотели : https://take.ms/VP7tA
const events$ = interval(300).pipe(
map(num => `Firing event ${num + 1}`)
);
const source = concat(events$.pipe(
tap(console.log),
take(10),
), timer(1000).pipe(switchMapTo(EMPTY)), events$.pipe(
tap(console.log),
take(10),
));
let lastTimer = 0;
const last$ = new Subject<number>();
merge(
source.pipe(
scan((state, event) => {
state[1] = null;
const stamp = new Date().getTime();
clearTimeout(lastTimer);
if (stamp - state[0] < 1000) {
lastTimer = setTimeout(() => last$.next(event), (stamp - state[0]) + 50);
return state;
}
state[0] = stamp;
state[1] = event;
return state;
}, [0, null]),
filter(([, event]) => event !== null),
map(([, event]) => event || 0),
),
last$,
).pipe(
map(x => `Received ${JSON.stringify(x)}!`)
).subscribe(x =>
console.log(
"%c" + x,
"background: purple; color: white; padding: 3px 5px; border-radius: 3px;"
)
);