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

updateMachine.ts

209 lines4.8 KB

1import type {
2 DesktopRuntimeInfo,
3 DesktopUpdateChannel,
4 DesktopUpdateReleaseNote,
5 DesktopUpdateState,
6} from "@t3tools/contracts";
7​
8export function nextStatusAfterDownloadFailure(
9 currentState: DesktopUpdateState,
10): DesktopUpdateState["status"] {
11 return currentState.availableVersion ? "available" : "error";
13​
14export function getCanRetryAfterDownloadFailure(currentState: DesktopUpdateState): boolean {
15 return currentState.availableVersion !== null;
17​
18export function createInitialDesktopUpdateState(
19 currentVersion: string,
20 runtimeInfo: DesktopRuntimeInfo,
21 channel: DesktopUpdateChannel,
22): DesktopUpdateState {
23 return {
24 enabled: false,
25 status: "disabled",
26 channel,
27 currentVersion,
28 hostArch: runtimeInfo.hostArch,
29 appArch: runtimeInfo.appArch,
30 runningUnderArm64Translation: runtimeInfo.runningUnderArm64Translation,
31 availableVersion: null,
32 downloadedVersion: null,
33 releaseNotes: [],
34 downloadPercent: null,
35 checkedAt: null,
36 message: null,
37 errorContext: null,
38 canRetry: false,
39 };
41​
42export function reduceDesktopUpdateStateOnCheckStart(
43 state: DesktopUpdateState,
44 checkedAt: string,
45): DesktopUpdateState {
46 const hasDownloadedUpdate = state.downloadedVersion !== null;
47 return {
48 ...state,
49 status: "checking",
50 checkedAt,
51 releaseNotes: hasDownloadedUpdate ? state.releaseNotes : [],
52 message: null,
53 downloadPercent: hasDownloadedUpdate ? 100 : null,
54 errorContext: null,
55 canRetry: false,
56 };
58​
59export function reduceDesktopUpdateStateOnCheckFailure(
60 state: DesktopUpdateState,
61 message: string,
62 checkedAt: string,
63): DesktopUpdateState {
64 if (state.downloadedVersion !== null) {
65 return {
66 ...state,
67 status: "downloaded",
68 message: null,
69 checkedAt,
70 downloadPercent: 100,
71 errorContext: null,
72 canRetry: true,
73 };
74 }
75​
76 return {
77 ...state,
78 status: "error",
79 message,
80 checkedAt,
81 downloadPercent: null,
82 errorContext: "check",
83 canRetry: true,
84 };
86​
87export function reduceDesktopUpdateStateOnUpdateAvailable(
88 state: DesktopUpdateState,
89 version: string,
90 checkedAt: string,
91 releaseNotes: ReadonlyArray<DesktopUpdateReleaseNote> = [],
92): DesktopUpdateState {
93 const isDownloadedVersion = state.downloadedVersion === version;
94 const nextReleaseNotes =
95 isDownloadedVersion && releaseNotes.length === 0 ? state.releaseNotes : releaseNotes;
96 return {
97 ...state,
98 status: isDownloadedVersion ? "downloaded" : "available",
99 availableVersion: version,
100 downloadedVersion: isDownloadedVersion ? version : null,
101 releaseNotes: nextReleaseNotes,
102 downloadPercent: isDownloadedVersion ? 100 : null,
103 checkedAt,
104 message: null,
105 errorContext: null,
106 canRetry: isDownloadedVersion,
107 };
109​
110export function reduceDesktopUpdateStateOnNoUpdate(
111 state: DesktopUpdateState,
112 checkedAt: string,
113): DesktopUpdateState {
114 if (state.downloadedVersion !== null) {
115 return {
116 ...state,
117 status: "downloaded",
118 availableVersion: state.downloadedVersion,
119 downloadPercent: 100,
120 checkedAt,
121 message: null,
122 errorContext: null,
123 canRetry: true,
124 };
125 }
126​
127 return {
128 ...state,
129 status: "up-to-date",
130 availableVersion: null,
131 downloadedVersion: null,
132 releaseNotes: [],
133 downloadPercent: null,
134 checkedAt,
135 message: null,
136 errorContext: null,
137 canRetry: false,
138 };
140​
141export function reduceDesktopUpdateStateOnDownloadStart(
142 state: DesktopUpdateState,
143): DesktopUpdateState {
144 return {
145 ...state,
146 status: "downloading",
147 downloadPercent: 0,
148 message: null,
149 errorContext: null,
150 canRetry: false,
151 };
153​
154export function reduceDesktopUpdateStateOnDownloadFailure(
155 state: DesktopUpdateState,
156 message: string,
157): DesktopUpdateState {
158 return {
159 ...state,
160 status: nextStatusAfterDownloadFailure(state),
161 message,
162 downloadPercent: null,
163 errorContext: "download",
164 canRetry: getCanRetryAfterDownloadFailure(state),
165 };
167​
168export function reduceDesktopUpdateStateOnDownloadProgress(
169 state: DesktopUpdateState,
170 percent: number,
171): DesktopUpdateState {
172 return {
173 ...state,
174 status: "downloading",
175 downloadPercent: percent,
176 message: null,
177 errorContext: null,
178 canRetry: false,
179 };
181​
182export function reduceDesktopUpdateStateOnDownloadComplete(
183 state: DesktopUpdateState,
184 version: string,
185): DesktopUpdateState {
186 return {
187 ...state,
188 status: "downloaded",
189 availableVersion: version,
190 downloadedVersion: version,
191 downloadPercent: 100,
192 message: null,
193 errorContext: null,
194 canRetry: true,
195 };
197​
198export function reduceDesktopUpdateStateOnInstallFailure(
199 state: DesktopUpdateState,
200 message: string,
201): DesktopUpdateState {
202 return {
203 ...state,
204 status: "downloaded",
205 message,
206 errorContext: "install",
207 canRetry: true,
208 };