|
| 1 | +import { once, listen, emit, UnlistenFn } from './helpers/event' |
| 2 | + |
| 3 | +export type UpdateStatus = 'PENDING' | 'ERROR' | 'DONE' | 'UPTODATE' |
| 4 | + |
| 5 | +export interface UpdateStatusResult { |
| 6 | + error?: string |
| 7 | + status: UpdateStatus |
| 8 | +} |
| 9 | + |
| 10 | +export interface UpdateManifest { |
| 11 | + version: string |
| 12 | + date: string |
| 13 | + body: string |
| 14 | +} |
| 15 | + |
| 16 | +export interface UpdateResult { |
| 17 | + manifest?: UpdateManifest |
| 18 | + shouldUpdate: boolean |
| 19 | +} |
| 20 | + |
| 21 | +export async function installUpdate(): Promise<void> { |
| 22 | + let unlistenerFn: UnlistenFn | undefined |
| 23 | + |
| 24 | + function cleanListener(): void { |
| 25 | + if (unlistenerFn) { |
| 26 | + unlistenerFn() |
| 27 | + } |
| 28 | + unlistenerFn = undefined |
| 29 | + } |
| 30 | + |
| 31 | + return new Promise((resolve, reject) => { |
| 32 | + function onStatusChange(statusResult: UpdateStatusResult): void { |
| 33 | + if (statusResult.error) { |
| 34 | + cleanListener() |
| 35 | + return reject(statusResult.error) |
| 36 | + } |
| 37 | + |
| 38 | + // install complete |
| 39 | + if (statusResult.status === 'DONE') { |
| 40 | + cleanListener() |
| 41 | + return resolve() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // listen status change |
| 46 | + listen('tauri://update-status', (data: { payload: any }) => { |
| 47 | + onStatusChange(data?.payload as UpdateStatusResult) |
| 48 | + }) |
| 49 | + .then((fn) => { |
| 50 | + unlistenerFn = fn |
| 51 | + }) |
| 52 | + .catch((e) => { |
| 53 | + cleanListener() |
| 54 | + // dispatch the error to our checkUpdate |
| 55 | + throw e |
| 56 | + }) |
| 57 | + |
| 58 | + // start the process we dont require much security as it's |
| 59 | + // handled by rust |
| 60 | + emit('tauri://update-install').catch((e) => { |
| 61 | + cleanListener() |
| 62 | + // dispatch the error to our checkUpdate |
| 63 | + throw e |
| 64 | + }) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +export async function checkUpdate(): Promise<UpdateResult> { |
| 69 | + let unlistenerFn: UnlistenFn | undefined |
| 70 | + |
| 71 | + function cleanListener(): void { |
| 72 | + if (unlistenerFn) { |
| 73 | + unlistenerFn() |
| 74 | + } |
| 75 | + unlistenerFn = undefined |
| 76 | + } |
| 77 | + |
| 78 | + return new Promise((resolve, reject) => { |
| 79 | + function onUpdateAvailable(manifest: UpdateManifest): void { |
| 80 | + cleanListener() |
| 81 | + return resolve({ |
| 82 | + manifest, |
| 83 | + shouldUpdate: true |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + function onStatusChange(statusResult: UpdateStatusResult): void { |
| 88 | + if (statusResult.error) { |
| 89 | + cleanListener() |
| 90 | + return reject(statusResult.error) |
| 91 | + } |
| 92 | + |
| 93 | + if (statusResult.status === 'UPTODATE') { |
| 94 | + cleanListener() |
| 95 | + return resolve({ |
| 96 | + shouldUpdate: false |
| 97 | + }) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // wait to receive the latest update |
| 102 | + once('tauri://update-available', (data: { payload: any }) => { |
| 103 | + onUpdateAvailable(data?.payload as UpdateManifest) |
| 104 | + }).catch((e) => { |
| 105 | + cleanListener() |
| 106 | + // dispatch the error to our checkUpdate |
| 107 | + throw e |
| 108 | + }) |
| 109 | + |
| 110 | + // listen status change |
| 111 | + listen('tauri://update-status', (data: { payload: any }) => { |
| 112 | + onStatusChange(data?.payload as UpdateStatusResult) |
| 113 | + }) |
| 114 | + .then((fn) => { |
| 115 | + unlistenerFn = fn |
| 116 | + }) |
| 117 | + .catch((e) => { |
| 118 | + cleanListener() |
| 119 | + // dispatch the error to our checkUpdate |
| 120 | + throw e |
| 121 | + }) |
| 122 | + |
| 123 | + // start the process |
| 124 | + emit('tauri://update').catch((e) => { |
| 125 | + cleanListener() |
| 126 | + // dispatch the error to our checkUpdate |
| 127 | + throw e |
| 128 | + }) |
| 129 | + }) |
| 130 | +} |
0 commit comments