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

remoteUpdateFlow.ts

120 lines4.0 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/**
15 * The caps are what stop a check -> up-to-date -> check loop and endless
16 * download retries; they are counts rather than booleans because a state event
17 * raced by the local 4-minute poller can re-show an already-handled status
18 * once.
19 */
20export interface RemoteDesktopUpdateAttempts {
21 readonly checks: number;
22 readonly downloads: number;
24​
25export const MAX_REMOTE_UPDATE_CHECKS = 2;
26export const MAX_REMOTE_UPDATE_DOWNLOADS = 3;
27​
28function isInstallableDesktopUpdateState(state: DesktopUpdateState): boolean {
29 return (
30 state.downloadedVersion !== null &&
31 (state.status === "downloaded" ||
32 (state.status === "error" &&
33 (state.errorContext === null || state.errorContext === "install")))
34 );
36​
37export function nextRemoteDesktopUpdateStep(
38 state: DesktopUpdateState,
39 attempts: RemoteDesktopUpdateAttempts,
40 disabledReason: string | null,
41): RemoteDesktopUpdateStep {
42 if (!state.enabled || state.status === "disabled") {
43 return {
44 action: "done",
45 outcome: "failed",
46 reason: disabledReason ?? "Automatic updates are disabled on this machine.",
47 };
48 }
49 // An install of this version already failed to apply here. Installing it
50 // again from afar would stop the app's threads for nothing, so only the user
51 // on this machine retries it.
52 if (
53 state.notAppliedVersion !== undefined &&
54 (state.downloadedVersion ?? state.availableVersion) === state.notAppliedVersion
55 ) {
56 return {
57 action: "done",
58 outcome: "failed",
59 reason: `Update ${state.notAppliedVersion} did not apply on this machine. Retry it from the desktop app there.`,
60 };
61 }
62 // Mirror installDownloadedUpdate's own admission rule exactly, so the run
63 // never reports an install that the updater then refuses. A download
64 // survives an unrelated background updater error (errorContext null) and
65 // a previous failed install (errorContext "install"); it does not survive
66 // a check or download error, which fall through to the error branch.
67 if (isInstallableDesktopUpdateState(state)) {
68 return { action: "install" };
69 }
70 if (state.status === "downloading" || state.status === "checking") {
71 return { action: "wait" };
72 }
73 if (state.status === "available") {
74 if (attempts.downloads >= MAX_REMOTE_UPDATE_DOWNLOADS) {
75 return {
76 action: "done",
77 outcome: "failed",
78 reason: state.message ?? "The desktop app failed to download the update.",
79 };
80 }
81 return { action: "download" };
82 }
83 // "up-to-date" and "error" are retained from earlier/background checks,
84 // so before this run has issued its own check they are stale, not
85 // terminal: the whole point of a remote request is to look again.
86 if (state.status === "up-to-date") {
87 if (attempts.checks === 0) {
88 return { action: "check" };
89 }
90 return { action: "done", outcome: "up-to-date" };
91 }
92 if (state.status === "error") {
93 if (attempts.checks === 0) {
94 return { action: "check" };
95 }
96 return {
97 action: "done",
98 outcome: "failed",
99 reason: state.message ?? "The desktop app update failed.",
100 };
101 }
102 if (attempts.checks >= MAX_REMOTE_UPDATE_CHECKS) {
103 return {
104 action: "done",
105 outcome: "failed",
106 reason: "The desktop app did not report an update result.",
107 };
108 }
109 return { action: "check" };
111​
112/**
113 * Updater messages are plain strings and may be blank; a blank reason would
114 * fail encoding and silently drop the terminal report, leaving the server to
115 * wait for its timeout.
116 */
117export function normalizeRemoteUpdateReason(reason: string | undefined): string | undefined {
118 const trimmed = reason?.trim();
119 return trimmed ? trimmed : undefined;