Skip to content

Commit

Permalink
fix: handle offline dolphin a bit better
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilNarayana committed Mar 25, 2024
1 parent c321aa9 commit 779867a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/dolphin/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class DolphinManager {
await this._updateDolphinFlags(dolphinDownloadInfo, dolphinType);
} catch (err) {
log.error(`Failed to fetch latest Dolphin version: ${err}`);
this._onOffline(dolphinType);
return;
}

Expand Down Expand Up @@ -229,6 +230,7 @@ export class DolphinManager {
await this._updateDolphinFlags(dolphinDownloadInfo, launchType);
} catch (err) {
log.error(`Failed to fetch latest Dolphin version: ${err}`);
this._onOffline(launchType);
return;
}
const installation = this.getInstallation(launchType);
Expand Down Expand Up @@ -326,6 +328,13 @@ export class DolphinManager {
});
}

private _onOffline(dolphinType: DolphinLaunchType) {
this.eventSubject.next({
type: DolphinEventType.OFFLINE,
dolphinType,
});
}

// Run after fetchLatestVersion to update the necessary flags
private async _updateDolphinFlags(downloadInfo: DolphinVersionResponse, dolphinType: DolphinLaunchType) {
const isBeta = (downloadInfo.version as string).includes("-beta");
Expand Down
7 changes: 7 additions & 0 deletions src/dolphin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export enum DolphinEventType {
DOWNLOAD_START = "DOWNLOAD_START",
DOWNLOAD_PROGRESS = "DOWNLOAD_PROGRESS",
DOWNLOAD_COMPLETE = "DOWNLOAD_COMPLETE",
OFFLINE = "OFFLINE",
}

export type DolphinNetplayClosedEvent = {
Expand Down Expand Up @@ -84,11 +85,17 @@ export type DolphinDownloadCompleteEvent = {
dolphinVersion: string | null;
};

export type DolphinOfflineEvent = {
type: DolphinEventType.OFFLINE;
dolphinType: DolphinLaunchType;
};

export type DolphinEventMap = {
[DolphinEventType.CLOSED]: DolphinNetplayClosedEvent | DolphinPlaybackClosedEvent;
[DolphinEventType.DOWNLOAD_START]: DolphinDownloadStartEvent;
[DolphinEventType.DOWNLOAD_PROGRESS]: DolphinDownloadProgressEvent;
[DolphinEventType.DOWNLOAD_COMPLETE]: DolphinDownloadCompleteEvent;
[DolphinEventType.OFFLINE]: DolphinOfflineEvent;
};

export type DolphinEvent = DolphinEventMap[DolphinEventType];
Expand Down
13 changes: 12 additions & 1 deletion src/renderer/lib/dolphin/use_dolphin_listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
DolphinDownloadProgressEvent,
DolphinDownloadStartEvent,
DolphinNetplayClosedEvent,
DolphinOfflineEvent,
DolphinPlaybackClosedEvent,
DolphinService,
} from "@dolphin/types";
Expand All @@ -22,7 +23,7 @@ import {

// Setup listeners for DolphinService
export const useDolphinListeners = (dolphinService: DolphinService) => {
const { showError } = useToasts();
const { showError, showWarning } = useToasts();
const dolphinClosedHandler = useCallback(
({ dolphinType, exitCode }: DolphinNetplayClosedEvent | DolphinPlaybackClosedEvent) => {
setDolphinOpened(dolphinType, false);
Expand All @@ -47,6 +48,14 @@ export const useDolphinListeners = (dolphinService: DolphinService) => {
setDolphinVersion(event.dolphinVersion, event.dolphinType);
}, []);

const dolphinOfflineHandler = useCallback(
(event: DolphinOfflineEvent) => {
showWarning("You seem to be offline, some functionality of the Launcher and Dolphin will be unavailable.");
setDolphinStatus(event.dolphinType, DolphinStatus.READY);
},
[showWarning],
);

const dolphinDownloadStartHandler = useCallback((event: DolphinDownloadStartEvent) => {
setDolphinStatus(event.dolphinType, DolphinStatus.DOWNLOADING);
}, []);
Expand All @@ -57,6 +66,7 @@ export const useDolphinListeners = (dolphinService: DolphinService) => {
subs.push(dolphinService.onEvent(DolphinEventType.DOWNLOAD_START, dolphinDownloadStartHandler));
subs.push(dolphinService.onEvent(DolphinEventType.DOWNLOAD_PROGRESS, dolphinProgressHandler));
subs.push(dolphinService.onEvent(DolphinEventType.DOWNLOAD_COMPLETE, dolphinCompleteHandler));
subs.push(dolphinService.onEvent(DolphinEventType.OFFLINE, dolphinOfflineHandler));

return () => {
subs.forEach((unsub) => unsub());
Expand All @@ -67,5 +77,6 @@ export const useDolphinListeners = (dolphinService: DolphinService) => {
dolphinProgressHandler,
dolphinCompleteHandler,
dolphinDownloadStartHandler,
dolphinOfflineHandler,
]);
};

0 comments on commit 779867a

Please sign in to comment.