Created
March 26, 2017 16:43
-
-
Save kouak/18910729ecfda906eadbd6084793f5eb to your computer and use it in GitHub Desktop.
Apollo store periodic reset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @flow */ | |
import React from 'react'; | |
import moment from 'moment'; | |
type Props = { | |
client: any, | |
}; | |
type State = { | |
nextReset: ?Date, | |
}; | |
const setNextReset = (prev: State): State => { | |
const nextReset = moment.utc().add(1, 'day').hours(2).toDate(); | |
console.debug('ApolloStoreResetter: Next apollo store reset will be', nextReset); | |
return ({ | |
...prev, | |
nextReset, | |
}); | |
}; | |
class ApolloStoreResetter extends React.Component<void, Props, State> { | |
interval: * = null; | |
state: State = { | |
nextReset: null, | |
}; | |
componentDidMount() { | |
this.interval = setInterval(this.checkTime, 1000 * 60); | |
this.setState(setNextReset); | |
} | |
componentWillUnmount() { | |
if(this.interval) { | |
clearInterval(this.interval); | |
} | |
} | |
checkTime = () => { | |
const { nextReset } = this.state; | |
const { client } = this.props; | |
if(nextReset && moment().isAfter(nextReset)) { | |
console.debug('ApolloStoreResetter: resetting store !'); | |
client.resetStore(); | |
this.setState(setNextReset); | |
} | |
}; | |
render() { | |
return null; | |
} | |
} | |
import { withApollo } from 'react-apollo'; | |
export default withApollo(ApolloStoreResetter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment