Created
August 17, 2018 08:28
-
-
Save slorber/f559152b74862230e36dca81dc563727 to your computer and use it in GitHub Desktop.
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
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