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

updateMachine.ts

166 lines3.5 KB

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