-
Notifications
You must be signed in to change notification settings - Fork 639
/
ajax-queue.ts
84 lines (72 loc) · 2.28 KB
/
ajax-queue.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
import AjaxRequest from '@typo3/core/ajax/ajax-request';
import { AjaxResponse } from '@typo3/core/ajax/ajax-response';
interface Payload {
url: string;
method?: string;
data?: { [key: string]: any},
onfulfilled: (value: AjaxResponse) => Promise<void>;
onrejected: (reason: string) => void;
finally?: () => void;
}
/**
* Module: @typo3/install/module/ajax-queue
*/
class AjaxQueue {
private requests: Array<AjaxRequest> = [];
private requestCount: number = 0;
private readonly threshold: number = 5;
private queue: Array<Payload> = [];
public add(payload: Payload): void {
this.queue.push(payload);
this.handleNext();
}
public flush(): void {
this.queue = [];
this.requests.forEach((request: AjaxRequest) => request.abort());
this.requests = [];
}
private handleNext(): void {
if (this.queue.length > 0 && this.requestCount < this.threshold) {
this.incrementRequestCount();
this.sendRequest(this.queue.shift()).finally((): void => {
this.decrementRequestCount();
this.handleNext();
});
}
}
private async sendRequest(payload: Payload): Promise<void> {
const request = new AjaxRequest(payload.url);
let response: Promise<AjaxResponse>;
if (typeof payload.method !== 'undefined' && payload.method.toUpperCase() === 'POST') {
response = request.post(payload.data);
} else {
response = request.withQueryArguments(payload.data || {}).get();
}
this.requests.push(request);
return response.then(payload.onfulfilled, payload.onrejected).then((): void => {
const idx = this.requests.indexOf(request);
this.requests.splice(idx, 1);
});
}
private incrementRequestCount(): void {
this.requestCount++;
}
private decrementRequestCount(): void {
if (this.requestCount > 0) {
this.requestCount--;
}
}
}
export default new AjaxQueue();