-
Notifications
You must be signed in to change notification settings - Fork 54
/
buffer-full-utilities.js
71 lines (62 loc) · 1.76 KB
/
buffer-full-utilities.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
58
59
60
61
62
63
64
65
66
67
68
69
70
let appendScript = (src, resolve) => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.onload = resolve;
document.body.appendChild(script);
}
let xhrScript = src => {
var xhr = new XMLHttpRequest();
xhr.open("GET", src, false);
xhr.send(null);
}
let waitForNextTask = () => {
return new Promise(resolve => {
step_timeout(resolve, 0);
});
};
let waitUntilConditionIsMet = cond => {
return new Promise(resolve => {
let checkCondition = function() {
if (cond.apply(null)) {
resolve();
} else {
step_timeout(checkCondition.bind(null,cond), 0);
}
}
step_timeout(checkCondition.bind(null, cond), 0);
});
}
let waitForEventToFire = () => {
return new Promise(resolve => {
let waitForIt = function() {
if (eventFired) {
eventFired = false;
resolve();
} else {
step_timeout(waitForIt, 0);
}
}
step_timeout(waitForIt, 0);
});
};
let clearBufferAndSetSize = size => {
performance.clearResourceTimings();
performance.setResourceTimingBufferSize(size);
}
let fillUpTheBufferWithSingleResource = src => {
return new Promise(resolve => {
// This resource gets buffered in the resource timing entry buffer.
appendScript(src, resolve);
});
};
let loadResource = src => {
return new Promise(resolve => {
appendScript(src, resolve);
});
};
let fillUpTheBufferWithTwoResources = async src => {
// These resources get buffered in the resource timing entry buffer.
await loadResource(src);
await loadResource(src + '?second');
};