Skip to content

Instantly share code, notes, and snippets.

@CharlieHess
Last active November 6, 2019 20:38
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 CharlieHess/28eee4e8f434bc8f868bf615145c640d to your computer and use it in GitHub Desktop.
Save CharlieHess/28eee4e8f434bc8f868bf615145c640d to your computer and use it in GitHub Desktop.
Example of prompting users when an update is available
/**
* 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