这是一个使用 reduce 和非终止 observable 的示例;
使用窗口时间:
import { fromEvent, interval, timer } from 'rxjs';
import { reduce, filter, windowTime, map, mergeMap } from 'rxjs/operators';
const interval$ = interval(1000);
const observable = interval$.pipe(
windowTime(2000), // each window is 2s
mergeMap(window$ => window$.pipe(
reduce((a,x) => { // reduce to array
return [...a,x];
}, []),
filter(x => !!x.length) // in the background timer is still running so suppress empty events
)), // flatten the Observable-of-Observables
);
const subscription = observable.subscribe(x => console.log(x));
setTimeout(() => subscription.unsubscribe(), 10000);
使用缓冲时间:
import { fromEvent } from 'rxjs';
import { bufferTime, filter, map } from 'rxjs/operators';
let count = 1;
const clicks = fromEvent(document, 'click');
const observable = clicks.pipe(
bufferTime(1000), // batch into array every 1s
filter(x => !!x.length), // ignore events without clicks
map(x => x.reduce((a,y) => ({...a, [count++]: y}), {})),
);
observable.subscribe(x => console.log(x));
使用审计时间:
import { fromEvent } from 'rxjs';
import { tap, auditTime, map } from 'rxjs/operators';
let buffer = [];
const clicks = fromEvent(document, 'click');
const observable = clicks.pipe(
tap((event) => buffer.push(event)),
auditTime(1000), // buffer every 1s after 1st click is detected
map((_lastEvent) => { // ignore last event
const events = buffer; // save off buffer
buffer = []; // clear buffer
return events.reduce((a,e,i) => ({...a, [i]: e}),{});
}),
);
observable.subscribe((events) => console.log(events));
使用 takeUntil 并重复:
注意:take/repeat 将重置 observable(即,interval-counter 保持在 0 并且事件可能会丢失)
import { fromEvent, timer, interval } from 'rxjs';
import { takeUntil, reduce, repeat, filter } from 'rxjs/operators';
const interval$ = interval(1000);
const timer$ = timer(2000);
const observable = interval$.pipe(
takeUntil(timer$), // unsubscribe from stream every 2s so reduce terminates
reduce((acc, event) => [...acc, event], []), // reduce to array of events
filter(x => !!x.length), // suppress emission of empty stream
repeat(), // resubscribe to stream
);
// console will only show array of [0] since takeUntil stops right when interval emits
const subscription = observable.subscribe(x => console.log(x));
setTimeout(() => subscription.unsubscribe(), 10000);