Last active
November 6, 2019 20:38
-
-
Save CharlieHess/28eee4e8f434bc8f868bf615145c640d to your computer and use it in GitHub Desktop.
Example of prompting users when an update is available
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
/** | |
* Shows a notification prompting users to restart when an update has been downloaded | |
* and we're ready to apply it. | |
*/ | |
export const promptToUpdateEpic: Epic<Action<ReleaseChannel | UpdateStatus>, State> = ( | |
action$, | |
store, | |
) => { | |
// Update status is redux state populated by autoUpdater events | |
const updateStatusChanged = action$.pipe( | |
map(() => getUpdateStatus(store)), | |
distinctUntilChanged() | |
); | |
// We'll want to cancel our timer if the release channel changes | |
const releaseChannelChanged = action$.pipe( | |
map(() => getReleaseChannel(store)), | |
distinctUntilChanged() | |
); | |
return combineLatest(updateStatusChanged, releaseChannelChanged).pipe( | |
filter(() => isUpdateSupported(store)), | |
switchMap(([ updateStatus, channel ]) => { | |
// Cancel the timer for any changes to the staged update | |
if (updateStatus !== UpdateStatus.UPDATE_DOWNLOADED) { | |
return empty<ReleaseChannel>(); | |
} | |
// Once downloaded, emit once, then remind at regular intervals | |
return timer(0, RESTART_REMINDER_INTERVAL).pipe(mapTo(channel)); | |
}), | |
map((channel) => updatePromptNotificationOptions(channel)), | |
// Dispatch this to a separate epic that handles notifications | |
map(newNotification) | |
); | |
}; | |
function updatePromptNotificationOptions( | |
channel: ReleaseChannel | |
): Electron.NotificationConstructorOptions { | |
return { | |
// … you get the idea | |
title: 'It’s a bird, it’s a tractor, it’s … a new version of Slack?' | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment