Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions apps/desktop/src/services/AppUpdateService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export class AppUpdateController {
private readonly now: () => string;
private initialized = false;
private unlistenProgress: UnlistenFn | null = null;
private checkRequestVersion = 0;
private channelTransitionQueue: Promise<void> = Promise.resolve();

constructor(deps: AppUpdateControllerDeps) {
this.native = deps.native;
Expand Down Expand Up @@ -109,11 +111,52 @@ export class AppUpdateController {
}
}

async setChannel(channel: AppUpdateChannel): Promise<void> {
await this.initialize();
await this.settings.updateAppUpdateChannel(channel);
await this.settings.updateAppUpdateLastCheckedAt(null);
this.commit({ type: 'channel-updated', channel });
setChannel(channel: AppUpdateChannel): Promise<void> {
if (this.initialized) {
return this.queueChannelTransition(channel);
}

return this.initialize().then(() => this.queueChannelTransition(channel));
}

private queueChannelTransition(channel: AppUpdateChannel): Promise<void> {
this.invalidateChecksForChannelTransition();
const transition = this.channelTransitionQueue.then(() =>
this.runChannelTransition(channel)
);
this.channelTransitionQueue = transition.catch(() => undefined);
return transition;
}

private async runChannelTransition(channel: AppUpdateChannel): Promise<void> {
const restoreState = this.invalidateChecksForChannelTransition();
const previousChannel = restoreState.channel;

try {
await this.settings.updateAppUpdateChannel(channel);
await this.settings.updateAppUpdateLastCheckedAt(null);
this.commit({ type: 'channel-updated', channel });
} catch (error) {
await this.settings.updateAppUpdateChannel(previousChannel).catch(() => undefined);
const persistedChannel = this.settings.getChannel();
this.replaceState(
persistedChannel === previousChannel
? restoreState
: reduceAppUpdateState(restoreState, {
type: 'channel-updated',
channel: persistedChannel,
})
);
throw error;
}
}

private invalidateChecksForChannelTransition(): AppUpdateState {
const restoreState: AppUpdateState =
this.state.status === 'checking' ? { ...this.state, status: 'idle' } : this.state;
this.checkRequestVersion += 1;
this.replaceState(restoreState);
return restoreState;
}

async checkNow(source: AppUpdateCheckSource = 'manual'): Promise<boolean> {
Expand All @@ -125,15 +168,24 @@ export class AppUpdateController {

const previousState = this.state;
const channel = this.state.channel;
const requestVersion = ++this.checkRequestVersion;
this.commit({ type: 'check-started', channel });

try {
const result = await this.native.checkForUpdates(channel);
const checkedAt = this.now();
if (!this.isCurrentCheckRequest(requestVersion, channel)) {
return false;
}

this.commit({ type: 'check-completed', channel, result, checkedAt });
await this.settings.updateAppUpdateLastCheckedAt(checkedAt);
return true;
} catch (error) {
if (!this.isCurrentCheckRequest(requestVersion, channel)) {
return false;
}

if (source === 'automatic') {
this.replaceState(previousState);
return false;
Expand Down Expand Up @@ -171,6 +223,10 @@ export class AppUpdateController {
}
}

private isCurrentCheckRequest(requestVersion: number, channel: AppUpdateChannel): boolean {
return requestVersion === this.checkRequestVersion && this.state.channel === channel;
}

private shouldRunAutomaticCheck(): boolean {
if (!this.state.autoCheckEnabled) {
return false;
Expand Down
Loading
Loading