-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
updater.ts
191 lines (168 loc) · 4.74 KB
/
updater.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
/**
* Customize the auto updater flow.
*
* This package is also accessible with `window.__TAURI__.updater` when `tauri.conf.json > build > withGlobalTauri` is set to true.
* @module
*/
import { once, listen, emit, UnlistenFn } from './event'
type UpdateStatus = 'PENDING' | 'ERROR' | 'DONE' | 'UPTODATE'
interface UpdateStatusResult {
error?: string
status: UpdateStatus
}
interface UpdateManifest {
version: string
date: string
body: string
}
interface UpdateResult {
manifest?: UpdateManifest
shouldUpdate: boolean
}
/**
* Listen to an updater event.
* @example
* ```typescript
* import { onUpdaterEvent } from "@tauri-apps/api/updater";
* const unlisten = await onUpdaterEvent(({ error, status }) => {
* console.log('Updater event', error, status);
* });
*
* // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
* unlisten();
* ```
*
* @param handler
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/
async function onUpdaterEvent(
handler: (status: UpdateStatusResult) => void
): Promise<UnlistenFn> {
return listen('tauri://update-status', (data: { payload: any }) => {
handler(data?.payload as UpdateStatusResult)
})
}
/**
* Install the update if there's one available.
* @example
* ```typescript
* import { checkUpdate, installUpdate } from '@tauri-apps/api/updater';
* const update = await checkUpdate();
* if (update.shouldUpdate) {
* console.log(`Installing update ${update.manifest?.version}, ${update.manifest?.date}, ${update.manifest.body}`);
* await installUpdate();
* }
* ```
*
* @return A promise indicating the success or failure of the operation.
*/
async function installUpdate(): Promise<void> {
let unlistenerFn: UnlistenFn | undefined
function cleanListener(): void {
if (unlistenerFn) {
unlistenerFn()
}
unlistenerFn = undefined
}
return new Promise((resolve, reject) => {
function onStatusChange(statusResult: UpdateStatusResult): void {
if (statusResult.error) {
cleanListener()
return reject(statusResult.error)
}
// install complete
if (statusResult.status === 'DONE') {
cleanListener()
return resolve()
}
}
// listen status change
onUpdaterEvent(onStatusChange)
.then((fn) => {
unlistenerFn = fn
})
.catch((e) => {
cleanListener()
// dispatch the error to our checkUpdate
throw e
})
// start the process we dont require much security as it's
// handled by rust
emit('tauri://update-install').catch((e) => {
cleanListener()
// dispatch the error to our checkUpdate
throw e
})
})
}
/**
* Checks if an update is available.
* @example
* ```typescript
* import { checkUpdate } from '@tauri-apps/api/updater';
* const update = await checkUpdate();
* // now run installUpdate() if needed
* ```
*
* @return Promise resolving to the update status.
*/
async function checkUpdate(): Promise<UpdateResult> {
let unlistenerFn: UnlistenFn | undefined
function cleanListener(): void {
if (unlistenerFn) {
unlistenerFn()
}
unlistenerFn = undefined
}
return new Promise((resolve, reject) => {
function onUpdateAvailable(manifest: UpdateManifest): void {
cleanListener()
return resolve({
manifest,
shouldUpdate: true
})
}
function onStatusChange(statusResult: UpdateStatusResult): void {
if (statusResult.error) {
cleanListener()
return reject(statusResult.error)
}
if (statusResult.status === 'UPTODATE') {
cleanListener()
return resolve({
shouldUpdate: false
})
}
}
// wait to receive the latest update
once('tauri://update-available', (data: { payload: any }) => {
onUpdateAvailable(data?.payload as UpdateManifest)
}).catch((e) => {
cleanListener()
// dispatch the error to our checkUpdate
throw e
})
// listen status change
onUpdaterEvent(onStatusChange)
.then((fn) => {
unlistenerFn = fn
})
.catch((e) => {
cleanListener()
// dispatch the error to our checkUpdate
throw e
})
// start the process
emit('tauri://update').catch((e) => {
cleanListener()
// dispatch the error to our checkUpdate
throw e
})
})
}
export type { UpdateStatus, UpdateStatusResult, UpdateManifest, UpdateResult }
export { onUpdaterEvent, installUpdate, checkUpdate }