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

updateMachine.ts

174 lines3.7 KB

1import type {
2 DesktopRuntimeInfo,
3 DesktopUpdateChannel,
4 DesktopUpdateState,
5} from "@t3tools/contracts";
6​
7export function nextStatusAfterDownloadFailure(
8 currentState: DesktopUpdateState,
9): DesktopUpdateState["status"] {
10 return currentState.availableVersion ? "available" : "error";
12​
13export function getCanRetryAfterDownloadFailure(currentState: DesktopUpdateState): boolean {
14 return currentState.availableVersion !== null;
16​
17export function createInitialDesktopUpdateState(
18 currentVersion: string,
19 runtimeInfo: DesktopRuntimeInfo,
20 channel: DesktopUpdateChannel,
21): DesktopUpdateState {
22 return {
23 enabled: false,
24 status: "disabled",
25 channel,
26 currentVersion,
27 hostArch: runtimeInfo.hostArch,
28 appArch: runtimeInfo.appArch,
29 runningUnderArm64Translation: runtimeInfo.runningUnderArm64Translation,
30 availableVersion: null,
31 downloadedVersion: null,
32 downloadPercent: null,
33 checkedAt: null,
34 message: null,
35 errorContext: null,
36 canRetry: false,
37 };
39​
40export function reduceDesktopUpdateStateOnCheckStart(
41 state: DesktopUpdateState,
42 checkedAt: string,
43): DesktopUpdateState {
44 return {
45 ...state,
46 status: "checking",
47 checkedAt,
48 message: null,
49 downloadPercent: null,
50 errorContext: null,
51 canRetry: false,
52 };
54​
55export function reduceDesktopUpdateStateOnCheckFailure(
56 state: DesktopUpdateState,
57 message: string,
58 checkedAt: string,
59): DesktopUpdateState {
60 return {
61 ...state,
62 status: "error",
63 message,
64 checkedAt,
65 downloadPercent: null,
66 errorContext: "check",
67 canRetry: true,
68 };
70​
71export function reduceDesktopUpdateStateOnUpdateAvailable(
72 state: DesktopUpdateState,
73 version: string,
74 checkedAt: string,
75): DesktopUpdateState {
76 return {
77 ...state,
78 status: "available",
79 availableVersion: version,
80 downloadedVersion: null,
81 downloadPercent: null,
82 checkedAt,
83 message: null,
84 errorContext: null,
85 canRetry: false,
86 };
88​
89export function reduceDesktopUpdateStateOnNoUpdate(
90 state: DesktopUpdateState,
91 checkedAt: string,
92): DesktopUpdateState {
93 return {
94 ...state,
95 status: "up-to-date",
96 availableVersion: null,
97 downloadedVersion: null,
98 downloadPercent: null,
99 checkedAt,
100 message: null,
101 errorContext: null,
102 canRetry: false,
103 };
105​
106export function reduceDesktopUpdateStateOnDownloadStart(
107 state: DesktopUpdateState,
108): DesktopUpdateState {
109 return {
110 ...state,
111 status: "downloading",
112 downloadPercent: 0,
113 message: null,
114 errorContext: null,
115 canRetry: false,
116 };
118​
119export function reduceDesktopUpdateStateOnDownloadFailure(
120 state: DesktopUpdateState,
121 message: string,
122): DesktopUpdateState {
123 return {
124 ...state,
125 status: nextStatusAfterDownloadFailure(state),
126 message,
127 downloadPercent: null,
128 errorContext: "download",
129 canRetry: getCanRetryAfterDownloadFailure(state),
130 };
132​
133export function reduceDesktopUpdateStateOnDownloadProgress(
134 state: DesktopUpdateState,
135 percent: number,
136): DesktopUpdateState {
137 return {
138 ...state,
139 status: "downloading",
140 downloadPercent: percent,
141 message: null,
142 errorContext: null,
143 canRetry: false,
144 };
146​
147export function reduceDesktopUpdateStateOnDownloadComplete(
148 state: DesktopUpdateState,
149 version: string,
150): DesktopUpdateState {
151 return {
152 ...state,
153 status: "downloaded",
154 availableVersion: version,
155 downloadedVersion: version,
156 downloadPercent: 100,
157 message: null,
158 errorContext: null,
159 canRetry: true,
160 };
162​
163export function reduceDesktopUpdateStateOnInstallFailure(
164 state: DesktopUpdateState,
165 message: string,
166): DesktopUpdateState {
167 return {
168 ...state,
169 status: "downloaded",
170 message,
171 errorContext: "install",
172 canRetry: true,
173 };