Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

remoteUpdateFlow.ts

111 lines3.6 KB

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;
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 );
31​
32export function offersNotAppliedVersion(state: DesktopUpdateState): boolean {
33 return (
34 state.notAppliedVersion !== undefined &&
35 (state.downloadedVersion ?? state.availableVersion) === state.notAppliedVersion
36 );
38​
39export function nextRemoteDesktopUpdateStep(
40 state: DesktopUpdateState,
41 attempts: RemoteDesktopUpdateAttempts,
42 disabledReason: string | null,
43): RemoteDesktopUpdateStep {
44 if (!state.enabled || state.status === "disabled") {
45 return {
46 action: "done",
47 outcome: "failed",
48 reason: disabledReason ?? "Automatic updates are disabled on this machine.",
49 };
50 }
51 // Reinstalling a failed version disrupts threads without updating; retries require the local user.
52 // The feed may have a replacement by now, so only refuse after this request's own check.
53 if (offersNotAppliedVersion(state)) {
54 if (attempts.checks === 0) {
55 return { action: "check" };
56 }
57 return {
58 action: "done",
59 outcome: "failed",
60 reason: `Update ${state.notAppliedVersion} did not apply on this machine. Retry it from the desktop app there.`,
61 };
62 }
63 // Match install admission, including retained downloads after unrelated background or earlier install errors.
64 if (isInstallableDesktopUpdateState(state)) {
65 return { action: "install" };
66 }
67 if (state.status === "downloading" || state.status === "checking") {
68 return { action: "wait" };
69 }
70 if (state.status === "available") {
71 if (attempts.downloads >= MAX_REMOTE_UPDATE_DOWNLOADS) {
72 return {
73 action: "done",
74 outcome: "failed",
75 reason: state.message ?? "The desktop app failed to download the update.",
76 };
77 }
78 return { action: "download" };
79 }
80 // Background up-to-date/error states are stale until this request has issued its own check.
81 if (state.status === "up-to-date") {
82 if (attempts.checks === 0) {
83 return { action: "check" };
84 }
85 return { action: "done", outcome: "up-to-date" };
86 }
87 if (state.status === "error") {
88 if (attempts.checks === 0) {
89 return { action: "check" };
90 }
91 return {
92 action: "done",
93 outcome: "failed",
94 reason: state.message ?? "The desktop app update failed.",
95 };
96 }
97 if (attempts.checks >= MAX_REMOTE_UPDATE_CHECKS) {
98 return {
99 action: "done",
100 outcome: "failed",
101 reason: "The desktop app did not report an update result.",
102 };
103 }
104 return { action: "check" };
106​
107/** Blank reasons fail encoding and drop the terminal report, forcing the server to time out. */
108export function normalizeRemoteUpdateReason(reason: string | undefined): string | undefined {
109 const trimmed = reason?.trim();
110 return trimmed ? trimmed : undefined;