forked from jamiebuilds/react-performance-observer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (46 loc) · 1.31 KB
/
index.js
File metadata and controls
58 lines (46 loc) · 1.31 KB
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
'use strict';
const REACT_PREFIX = '\u269B ';
const WARNING_PREFIX = '\u26D4 ';
const RENDER_LIFECYCLE_NAME = /^(\w+) \[(\w+)\]( Warning: (.*))?$/;
const LIFECYCLE_METHOD_NAME = /^(\w+)\.(\w+)( Warning: (.*))?$/;
function parseEntry(entry) {
let name = entry.name;
let isReact = name.startsWith(REACT_PREFIX);
let isWarning = name.startsWith(WARNING_PREFIX);
if (isReact || isWarning) {
let normalized = name.replace(isReact ? REACT_PREFIX : WARNING_PREFIX, '');
let match =
normalized.match(RENDER_LIFECYCLE_NAME) ||
normalized.match(LIFECYCLE_METHOD_NAME);
if (match) {
return {
name,
componentName: match[1],
phase: match[2],
warning: isWarning ? (match[4] || true) : null,
entryType: entry.entryType,
startTime: entry.startTime,
duration: entry.duration,
};
}
}
return null;
}
function observe(callback) {
let observer = new window.PerformanceObserver(list => {
let entries = list.getEntries();
let measurements = [];
entries.forEach(entry => {
let match = parseEntry(entry);
if (match) {
measurements.push(match);
}
});
callback(measurements);
});
observer.observe({
entryTypes: ['measure'],
});
}
exports.parseEntry = parseEntry;
exports.observe = observe;