Skip to content

Commit e6fa866

Browse files
committed
fixed an issue with initialization process
1 parent ad55c0d commit e6fa866

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-perf-devtool",
3-
"version": "3.0.8",
3+
"version": "3.1.8-beta.1",
44
"description": "A devtool for inspecting the performance of React Components",
55
"main": "index.js",
66
"types": "index.d.ts",

src/extension/components/ReactPerfDevtool.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ let queries = {
1919
measures: 'JSON.stringify(__REACT_PERF_DEVTOOL_GLOBAL_STORE__.measures)',
2020
rawMeasures:
2121
'JSON.stringify(__REACT_PERF_DEVTOOL_GLOBAL_STORE__.rawMeasures)',
22+
getTimeoutValue:
23+
'JSON.stringify(__REACT_PERF_DEVTOOL_GLOBAL_STORE__.timeout)',
2224
clear: `__REACT_PERF_DEVTOOL_GLOBAL_STORE__ = {
2325
length: 0,
2426
measures: [],
2527
rawMeasures: [],
28+
timeout: __REACT_PERF_DEVTOOL_GLOBAL_STORE__.timeout
2629
}`
2730
}
2831

32+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
33+
2934
/**
3035
This is the main component that renders the table, containing information about
3136
the component mount/render/update/unmount time and also lifecycle time.
@@ -67,8 +72,26 @@ export class ReactPerfDevtool extends React.Component {
6772
componentDidMount() {
6873
this.setState({ loading: true, showChart: true })
6974

70-
// Get the total measures and flush them if the store is empty.
71-
this.timer = setInterval(() => this.getMeasuresLength(), 2000)
75+
// Defer the initialization of the extension until the application loads. Why ?
76+
// Because some of the applications may be huge in size and take a lot of time to load.
77+
// If the extension initialization process kicks-in before the app loads, we are trapped inside the error state.
78+
// With this, users can configure a timeout value for initialization of the extension using the observer hook
79+
// Default value for the timeout is 2 sec.
80+
81+
// We need to resolve the promise after one sec. to make sure we have the updated __REACT_PERF_DEVTOOL_GLOBAL_STORE__ object otherwise we might end up with null
82+
sleep(1000).then(res => {
83+
this.evaluate(queries['getTimeoutValue'], (timeout, err) => {
84+
if (err) {
85+
this.setErrorState()
86+
return
87+
}
88+
89+
this.timer = setInterval(
90+
() => this.getMeasuresLength(),
91+
JSON.parse(timeout)
92+
)
93+
})
94+
})
7295
}
7396

7497
componentWillUnmount() {

src/npm/hook.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const registerObserver = (params, callback) => {
3434

3535
// TODO: Is there any way to polyfill this API ?
3636
if (window.PerformanceObserver) {
37-
const { shouldLog, port, components } = params
37+
const { shouldLog, port, components, timeout = 2000 } = params
3838

3939
let observer = new window.PerformanceObserver(list => {
4040
const measures = generateDataFromMeasures(
@@ -47,6 +47,7 @@ const registerObserver = (params, callback) => {
4747

4848
window.__REACT_PERF_DEVTOOL_GLOBAL_STORE__ = {
4949
measures,
50+
timeout,
5051
length: list.getEntries().length,
5152
rawMeasures: list.getEntries()
5253
}

0 commit comments

Comments
 (0)