/
util.js
130 lines (111 loc) · 2.93 KB
/
util.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// import afterFrame from "../node_modules/afterframe/dist/afterframe.module.js";
import afterFrame from 'afterframe';
export { afterFrame };
export const measureName = 'duration';
let promise = null;
export function afterFrameAsync() {
if (promise === null) {
promise = new Promise(resolve =>
afterFrame(time => {
promise = null;
resolve(time);
})
);
}
return promise;
}
export function measureMemory() {
if ('gc' in window && 'memory' in performance) {
// Report results in MBs
window.gc();
window.usedJSHeapSize = performance.memory.usedJSHeapSize / 1e6;
} else {
window.usedJSHeapSize = 0;
}
}
export function markRunStart(runId) {
performance.mark(`run-${runId}-start`);
}
let staticPromise = Promise.resolve();
export function markRunEnd(runId) {
return staticPromise.then(() => {
performance.mark(`run-${runId}-end`);
performance.measure(
`run-${runId}`,
`run-${runId}-start`,
`run-${runId}-end`
);
});
}
export function getRowIdSel(index) {
return `tbody > tr:nth-child(${index}) > td:first-child`;
}
export function getRowLinkSel(index) {
return `tbody > tr:nth-child(${index}) > td:nth-child(2) > a`;
}
/**
* @param {string} selector
* @returns {Element}
*/
export function getBySelector(selector) {
const element = document.querySelector(selector);
if (element == null) {
throw new Error(`Could not find element matching selector: ${selector}`);
}
return element;
}
export function testElement(selector) {
const testElement = document.querySelector(selector);
if (testElement == null) {
throw new Error(
'Test failed. Rendering after one paint was not successful'
);
}
}
export function testElementText(selector, expectedText) {
const elm = document.querySelector(selector);
if (elm == null) {
throw new Error('Could not find element matching selector: ' + selector);
}
if (elm.textContent != expectedText) {
throw new Error(
`Element did not have expected text. Expected: '${expectedText}' Actual: '${elm.textContent}'`
);
}
}
export function testElementTextContains(selector, expectedText) {
const elm = getBySelector(selector);
if (!elm.textContent.includes(expectedText)) {
throw new Error(
`Element did not include expected text. Expected to include: '${expectedText}' Actual: '${elm.textContent}'`
);
}
}
let count = 0;
const channel = new MessageChannel();
const callbacks = new Map();
channel.port1.onmessage = e => {
let id = e.data;
let fn = callbacks.get(id);
callbacks.delete(id);
fn();
};
let pm = function(callback) {
let id = ++count;
callbacks.set(id, callback);
this.postMessage(id);
}.bind(channel.port2);
export function nextTick() {
return new Promise(r => pm(r));
}
export function mutateAndLayoutAsync(mutation, times = 1) {
return new Promise(resolve => {
requestAnimationFrame(() => {
for (let i = 0; i < times; i++) {
mutation(i);
}
pm(resolve);
});
});
}
export const sleep = ms => new Promise(r => setTimeout(r, ms));