-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
57 lines (54 loc) · 1.46 KB
/
index.js
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
class PendingXHR {
constructor(page) {
this.page = page;
this.resourceType = 'xhr';
// page.setRequestInterception(true);
this.pendingXhrs = new Set();
this.finishedWithSuccessXhrs = new Set();
this.finishedWithErrorsXhrs = new Set();
page.on('request', request => {
if (request.resourceType() === this.resourceType) {
this.pendingXhrs.add(request);
if (!this.promise) {
this.promise = new Promise(resolve => {
this.resolver = resolve;
});
}
}
});
page.on('requestfailed', request => {
if (request.resourceType() === this.resourceType) {
this.pendingXhrs.delete(request);
this.finishedWithErrorsXhrs.add(request);
if (this.resolver) {
this.resolver();
delete this.promise;
delete this.resolver;
}
}
});
page.on('requestfinished', request => {
if (request.resourceType() === this.resourceType) {
this.pendingXhrs.delete(request);
this.finishedWithSuccessXhrs.add(request);
if (this.resolver) {
this.resolver();
delete this.promise;
delete this.resolver;
}
}
});
}
async waitForAllXhrFinished() {
if (this.pendingXhrCount() === 0) {
return true;
}
if (this.promise) {
await this.promise;
}
}
pendingXhrCount() {
return this.pendingXhrs.size;
}
}
module.exports.PendingXHR = PendingXHR;