Open History to see the last commit
1import type { DesktopUpdateRemoteOutcome, DesktopUpdateState } from "@t3tools/contracts";
2
3export type RemoteDesktopUpdateStep =
4 | { readonly action: "check" }
5 | { readonly action: "download" }
6 | { readonly action: "install" }
7 | { readonly action: "wait" }
8 | {
9 readonly action: "done";
10 readonly outcome: DesktopUpdateRemoteOutcome;
11 readonly reason?: string;
12 };
13
14/** Bound retries with counts: background poller races can repeat an already-handled status once. */
15export interface RemoteDesktopUpdateAttempts {
16 readonly checks: number;
17 readonly downloads: number;
18}
19
20const MAX_REMOTE_UPDATE_CHECKS = 2;
21export const MAX_REMOTE_UPDATE_DOWNLOADS = 3;
22
23function isInstallableDesktopUpdateState(state: DesktopUpdateState): boolean {
24 return (
25 state.downloadedVersion !== null &&
26 (state.status === "downloaded" ||
27 (state.status === "error" &&
28 (state.errorContext === null || state.errorContext === "install")))
29 );
30}
31
32export function nextRemoteDesktopUpdateStep(
33 state: DesktopUpdateState,
34 attempts: RemoteDesktopUpdateAttempts,
35 disabledReason: string | null,
36): RemoteDesktopUpdateStep {
37 if (!state.enabled || state.status === "disabled") {
38 return {
39 action: "done",
40 outcome: "failed",
41 reason: disabledReason ?? "Automatic updates are disabled on this machine.",
42 };
43 }
44 // Reinstalling a failed version disrupts threads without updating; retries require the local user.
45 if (
46 state.notAppliedVersion !== undefined &&
47 (state.downloadedVersion ?? state.availableVersion) === state.notAppliedVersion
48 ) {
49 return {
50 action: "done",
51 outcome: "failed",
52 reason: `Update ${state.notAppliedVersion} did not apply on this machine. Retry it from the desktop app there.`,
53 };
54 }
55 // Match install admission, including retained downloads after unrelated background or earlier install errors.
56 if (isInstallableDesktopUpdateState(state)) {
57 return { action: "install" };
58 }
59 if (state.status === "downloading" || state.status === "checking") {
60 return { action: "wait" };
61 }
62 if (state.status === "available") {
63 if (attempts.downloads >= MAX_REMOTE_UPDATE_DOWNLOADS) {
64 return {
65 action: "done",
66 outcome: "failed",
67 reason: state.message ?? "The desktop app failed to download the update.",
68 };
69 }
70 return { action: "download" };
71 }
72 // Background up-to-date/error states are stale until this request has issued its own check.
73 if (state.status === "up-to-date") {
74 if (attempts.checks === 0) {
75 return { action: "check" };
76 }
77 return { action: "done", outcome: "up-to-date" };
78 }
79 if (state.status === "error") {
80 if (attempts.checks === 0) {
81 return { action: "check" };
82 }
83 return {
84 action: "done",
85 outcome: "failed",
86 reason: state.message ?? "The desktop app update failed.",
87 };
88 }
89 if (attempts.checks >= MAX_REMOTE_UPDATE_CHECKS) {
90 return {
91 action: "done",
92 outcome: "failed",
93 reason: "The desktop app did not report an update result.",
94 };
95 }
96 return { action: "check" };
97}
98
99/** Blank reasons fail encoding and drop the terminal report, forcing the server to time out. */
100export function normalizeRemoteUpdateReason(reason: string | undefined): string | undefined {
101 const trimmed = reason?.trim();
102 return trimmed ? trimmed : undefined;
103}