mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-06 16:33:15 +01:00
25 lines
550 B
JavaScript
25 lines
550 B
JavaScript
import {StreamBase} from './base';
|
|
|
|
export class ThrottleStream extends StreamBase {
|
|
|
|
constructor(stream, delay = 0, accumulator = v => v) {
|
|
super();
|
|
this.stream = stream;
|
|
this.delay = delay;
|
|
this.accumulator = accumulator;
|
|
}
|
|
|
|
attach(observer) {
|
|
let scheduled = false;
|
|
let value = undefined;
|
|
return this.stream.attach(val => {
|
|
value = this.accumulator(val);
|
|
if (!scheduled) {
|
|
setTimeout(() => {
|
|
scheduled = false;
|
|
observer(value);
|
|
});
|
|
}
|
|
}, this.delay)
|
|
}
|
|
}
|