Skip to content

Commit

Permalink
Removing online offline event Handlers in componentWillUnmount
Browse files Browse the repository at this point in the history
  • Loading branch information
harshitkumar31 committed Aug 11, 2017
1 parent 4bfe051 commit 4fae889
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/asyncDataLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,26 @@ const AsyncDataLoader = (WrappedComponent, { componentName, refreshInterval, wra
* Fetch is invoked here to ensure server side rendering
* If refreshInterval is specified, we invoke setInterval.
*/
onOnlineEvent = (e) => {
this.startTimer = setInterval(this.fetch, refreshInterval);
}

onOfflineEvent = (e) => {
clearInterval(this.startTimer);
}

componentWillMount() {
this.fetch();
}
componentDidMount() {
if (refreshInterval) {
this.startTimer = setInterval(this.fetch, refreshInterval);
// Clear timer if browser goes offline
window.addEventListener('offline', (e) => {
clearInterval(this.startTimer);
});
window.addEventListener('offline', this.onOfflineEvent);

// Restart timer if browser goes online
window.addEventListener('online', this.onOnlineEvent);

// Restart timer if browser goes online
window.addEventListener('online', (e) => {
this.startTimer = setInterval(this.fetch, refreshInterval);
});
}
}
/**
Expand All @@ -106,6 +111,8 @@ const AsyncDataLoader = (WrappedComponent, { componentName, refreshInterval, wra
if (this.startTimer) {
clearInterval(this.startTimer);
}
window.removeEventListener('online', this.onOnlineEvent);
window.removeEventListener('offline', this.onOfflineEvent);
const obj = {};
obj[componentName] = {
loaded: LOADED_FALSE,
Expand Down

0 comments on commit 4fae889

Please sign in to comment.