Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

WithHealth: Wait 2s before showing not synced (fix #262) #263

Merged
merged 1 commit into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,29 @@ class RequireHealthOverlay extends Component {
};

state = {
visible: false // false | true | id of the timeout that will make the overlay visible
// We want to display the overlay after 2s of problematic health. Syncing
// to new blocks from the top of the chain takes 1s and we don't want to
// display the overlay in that case.
visible: false
};

componentDidMount () {
// Initial render check. Display overlay immediately if problematic health.
if (!statusMatches(this.props.health.status, this.props.require)) {
this.setState({ visible: true });
}
this.updateVisibility();
}

componentDidUpdate () {
if (statusMatches(this.props.health.status, this.props.require)) {
// If there is an ongoing timeout to make the overlay visible, clear it
if (typeof this.state.visible === 'number') {
clearTimeout(this.state.visible);
}
this.updateVisibility();
}

updateVisibility = () => {
if (statusMatches(this.props.health.status, this.props.require)) {
if (this.state.visible !== false) {
this.setState({ visible: false });
}
} else {
// Bad node health
// Display the overlay after 2s (if there is no ongoing timeout)
if (this.state.visible === false) {
this.setState({
visible: setTimeout(() => {
this.setState({ visible: true });
}, 2000)
});
this.setState({ visible: true });
}
}
}

componentWillUnmount () {
if (typeof this.state.visible === 'number') {
clearTimeout(this.state.visible);
}
}

render () {
const { visible } = this.state;
const { fullscreen } = this.props;
Expand Down
10 changes: 8 additions & 2 deletions packages/fether-react/src/utils/withHealth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
// SPDX-License-Identifier: BSD-3-Clause

import BigNumber from 'bignumber.js';
import { combineLatest, Observable, fromEvent, merge } from 'rxjs';
import { combineLatest, Observable, of, fromEvent, merge } from 'rxjs';
import { compose, mapPropsStream } from 'recompose';
import {
delay,
distinctUntilChanged,
filter,
map,
Expand Down Expand Up @@ -107,7 +108,12 @@ const rpcs$ = isApiConnected$.pipe(
startingBlock
}
};
})
}),
// Emit "not synced" only if we haven't been synced for over 2 seconds,
// as syncing to new blocks from the top of the chain usually takes ~1s.
// syncStatus$() is distinctUntilChanged, so {isSync: false} will never
// be fired twice in a row.
switchMap(sync => sync.isSync ? of(sync) : of(sync).pipe(delay(2000)))
),
peerCount$().pipe(withoutLoading())
)
Expand Down