/
ThrottleNode.ts
42 lines (38 loc) · 1.23 KB
/
ThrottleNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { DataFrame } from '../../data/DataFrame';
import { BufferOptions } from './BufferNode';
import { MemoryBufferNode } from './MemoryBufferNode';
/**
* @category Flow shape
*/
export class ThrottleNode<InOut extends DataFrame> extends MemoryBufferNode<InOut> {
private _pushReady = true;
constructor(options?: BufferOptions) {
super(options);
this.on('push', this.onThrottlePush.bind(this));
}
public onThrottlePush(): Promise<void> {
return this._handlePush();
}
private _handlePush(): Promise<void> {
return new Promise((resolve, reject) => {
if (this._pushReady) {
this._pushReady = false;
this.onPull()
.then(() => {
// Ready
this._pushReady = true;
return this.service.count();
})
.then((count) => {
if (count > 0) {
setTimeout(this._handlePush.bind(this), 10);
}
resolve();
})
.catch(reject);
} else {
resolve();
}
});
}
}