Skip to content

Instantly share code, notes, and snippets.

@slorber
Created August 17, 2018 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slorber/f559152b74862230e36dca81dc563727 to your computer and use it in GitHub Desktop.
Save slorber/f559152b74862230e36dca81dc563727 to your computer and use it in GitHub Desktop.
const setupAppOverTheAirUpdates = () => {
const checkForUpdates = async () => {
if (__DEV__) {
console.debug('checkForUpdates disabled in DEV');
return;
}
try {
const update = await Updates.checkForUpdateAsync();
console.debug('checkForUpdates result', update);
if (update.isAvailable) {
await Updates.fetchUpdateAsync();
AlertTranslated(
{ id: 'newVersion.alert.title' },
{ id: 'newVersion.alert.message' },
[
{
text: { id: 'newVersion.alert.cancel' },
onPress: () => {},
style: 'cancel',
},
{
text: { id: 'newVersion.alert.update' },
onPress: () => Updates.reloadFromCache(),
},
],
{ cancelable: true },
);
}
} catch (e) {
reportError(e);
}
};
// Not very useful to check for updates more than once every 2 minutes
const checkForUpdatesThrottled = throttle(checkForUpdates, 2 * 60 * 1000);
checkForUpdatesThrottled(); // Check on app startup
// We use a state change listener instead of setInterval
// See https://github.com/facebook/react-native/issues/12981
addAppResumeListener(checkForUpdatesThrottled);
Updates.addListener(event => {
console.debug('Updates event', event);
if (event.type === Updates.EventType.DOWNLOAD_STARTED) {
displayInfoToast({ id: 'newVersion.toast.downloadStarted' });
} else if (event.type === Updates.EventType.ERROR) {
reportMessage(`Unable to update app: ${event.message}`);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment