blob: 301aec8d52498c5b1ed4a45e128d953dd9187c88 (
plain) (
blame)
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
43
44
45
46
47
48
49
50
51
52
53
|
import * as _ from 'lodash';
import * as timers from 'timers';
import * as Web3 from 'web3';
// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang
// because they are using the wrong XHR package.
// Issue: https://github.com/trufflesuite/truffle-contract/issues/14
delete (global as any).XMLHttpRequest;
const MAX_QUEUE_SIZE = 500;
const DEFAULT_QUEUE_INTERVAL_MS = 1000;
export class RequestQueue {
protected queueIntervalMs: number;
protected queue: string[];
protected queueIntervalId: NodeJS.Timer;
protected web3: Web3;
constructor(web3: any) {
this.queueIntervalMs = DEFAULT_QUEUE_INTERVAL_MS;
this.queue = [];
this.web3 = web3;
this.start();
}
public add(recipientAddress: string): boolean {
if (this.isFull()) {
return false;
}
this.queue.push(recipientAddress);
return true;
}
public size(): number {
return this.queue.length;
}
public isFull(): boolean {
return this.size() >= MAX_QUEUE_SIZE;
}
protected start() {
this.queueIntervalId = timers.setInterval(() => {
if (this.queue.length === 0) {
return;
}
const recipientAddress = this.queue.shift();
this.processNextRequestFireAndForgetAsync(recipientAddress);
}, this.queueIntervalMs);
}
protected stop() {
clearInterval(this.queueIntervalId);
}
protected async processNextRequestFireAndForgetAsync(recipientAddress: string) {
throw new Error('Expected processNextRequestFireAndForgetAsync to be implemented by a superclass');
}
}
|