Unverified Commit 4b598dd1 authored by Lijiaoa's avatar Lijiaoa Committed by GitHub
Browse files

fix bug of refresh from disable refresh to refresh (#2274)

parent 470caf4f
...@@ -17,8 +17,9 @@ interface AppState { ...@@ -17,8 +17,9 @@ interface AppState {
} }
class App extends React.Component<{}, AppState> { class App extends React.Component<{}, AppState> {
private timerId!: number | null; private timerId!: number | undefined;
private dataFormatimer!: number; private dataFormatimer!: number;
private firstLoad: boolean = false; // when click refresh selector options
constructor(props: {}) { constructor(props: {}) {
super(props); super(props);
...@@ -66,14 +67,20 @@ class App extends React.Component<{}, AppState> { ...@@ -66,14 +67,20 @@ class App extends React.Component<{}, AppState> {
} }
} }
} }
changeInterval = (interval: number): void => { changeInterval = (interval: number): void => {
this.setState({ interval });
if (this.timerId === null && interval !== 0) { window.clearTimeout(this.timerId);
window.setTimeout(this.refresh); if (interval === 0) {
} else if (this.timerId !== null && interval === 0) { return;
window.clearTimeout(this.timerId);
} }
// setState will trigger page refresh at once.
// setState is asyc, interval not update to (this.state.interval) at once.
this.setState({interval}, () => {
this.firstLoad = true;
this.refresh();
});
} }
// TODO: use local storage // TODO: use local storage
...@@ -123,24 +130,30 @@ class App extends React.Component<{}, AppState> { ...@@ -123,24 +130,30 @@ class App extends React.Component<{}, AppState> {
} }
private refresh = async (): Promise<void> => { private refresh = async (): Promise<void> => {
const [experimentUpdated, trialsUpdated] = await Promise.all([EXPERIMENT.update(), TRIALS.update()]);
if (experimentUpdated) { // resolve this question: 10s -> 20s, page refresh twice.
this.setState(state => ({ experimentUpdateBroadcast: state.experimentUpdateBroadcast + 1 })); // only refresh this page after clicking the refresh options
} if (this.firstLoad !== true) {
if (trialsUpdated) { const [experimentUpdated, trialsUpdated] = await Promise.all([EXPERIMENT.update(), TRIALS.update()]);
this.setState(state => ({ trialsUpdateBroadcast: state.trialsUpdateBroadcast + 1 })); if (experimentUpdated) {
this.setState(state => ({ experimentUpdateBroadcast: state.experimentUpdateBroadcast + 1 }));
}
if (trialsUpdated) {
this.setState(state => ({ trialsUpdateBroadcast: state.trialsUpdateBroadcast + 1 }));
}
} else {
this.firstLoad = false;
} }
if (['DONE', 'ERROR', 'STOPPED'].includes(EXPERIMENT.status)) { if (['DONE', 'ERROR', 'STOPPED'].includes(EXPERIMENT.status)) {
// experiment finished, refresh once more to ensure consistency // experiment finished, refresh once more to ensure consistency
if (this.state.interval > 0) { this.setState({ interval: 0 });
this.setState({ interval: 0 }); this.lastRefresh();
this.lastRefresh(); return;
}
} else if (this.state.interval !== 0) {
this.timerId = window.setTimeout(this.refresh, this.state.interval * 1000);
} }
this.timerId = window.setTimeout(this.refresh, this.state.interval * 1000);
} }
public async lastRefresh(): Promise<void> { public async lastRefresh(): Promise<void> {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment