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

updateMachine.ts

170 lines3.7 KB

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