Skip to content

Commit 754f076

Browse files
fix(desktop): in-memory update progress polling (hoppscotch#5126)
Co-authored-by: jamesgeorge007 <[email protected]>
1 parent 936b0ff commit 754f076

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

packages/hoppscotch-desktop/src/types/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,4 @@ export interface UpdateState {
3131
status: UpdateStatus;
3232
version?: string;
3333
message?: string;
34-
progress?: {
35-
downloaded: number;
36-
total?: number;
37-
};
3834
}

packages/hoppscotch-desktop/src/utils/updater.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { type LazyStore } from "@tauri-apps/plugin-store";
44
import { UpdateStatus, CheckResult, UpdateState } from "~/types";
55

66
export class UpdaterService {
7+
private currentProgress: { downloaded: number; total?: number } = { downloaded: 0 };
8+
79
constructor(private store: LazyStore) {}
810

911
async initialize(): Promise<void> {
@@ -12,6 +14,10 @@ export class UpdaterService {
1214
});
1315
}
1416

17+
getCurrentProgress(): { downloaded: number; total?: number } {
18+
return this.currentProgress;
19+
}
20+
1521
async checkForUpdates(timeout = 5000): Promise<CheckResult> {
1622
try {
1723
await this.saveUpdateState({
@@ -86,41 +92,36 @@ export class UpdaterService {
8692
throw new Error("No update available to install");
8793
}
8894

95+
let totalBytes: number | undefined;
96+
let downloadedBytes = 0;
97+
8998
await this.saveUpdateState({
9099
status: UpdateStatus.DOWNLOADING,
91-
progress: {
92-
downloaded: 0,
93-
total: undefined
94-
}
95100
});
96101

97-
let totalBytes: number | undefined;
98-
let downloadedBytes = 0;
99-
100102
await updateResult.downloadAndInstall(
101103
(event: DownloadEvent) => {
102-
if (event.event === 'Started') {
103-
totalBytes = event.data.contentLength;
104-
this.saveUpdateState({
105-
status: UpdateStatus.DOWNLOADING,
106-
progress: {
107-
downloaded: 0,
108-
total: totalBytes
109-
}
110-
});
111-
} else if (event.event === 'Progress') {
112-
downloadedBytes += event.data.chunkLength;
113-
this.saveUpdateState({
114-
status: UpdateStatus.DOWNLOADING,
115-
progress: {
104+
try {
105+
if (event.event === 'Started') {
106+
totalBytes = event.data.contentLength;
107+
downloadedBytes = 0;
108+
console.log(`Download started, total size: ${totalBytes} bytes`);
109+
} else if (event.event === 'Progress') {
110+
downloadedBytes += event.data.chunkLength;
111+
console.log(`Download progress: ${downloadedBytes}/${totalBytes} bytes`);
112+
113+
this.currentProgress = {
116114
downloaded: downloadedBytes,
117115
total: totalBytes
118-
}
119-
});
120-
} else if (event.event === 'Finished') {
121-
this.saveUpdateState({
122-
status: UpdateStatus.INSTALLING
123-
});
116+
};
117+
} else if (event.event === 'Finished') {
118+
console.log("Download finished, starting installation");
119+
this.saveUpdateState({
120+
status: UpdateStatus.INSTALLING
121+
});
122+
}
123+
} catch (error) {
124+
console.warn('Progress tracking error:', error);
124125
}
125126
}
126127
);

packages/hoppscotch-desktop/src/views/Home.vue

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
</template>
130130

131131
<script setup lang="ts">
132-
import { ref, onMounted } from "vue"
132+
import { ref, onMounted, onUnmounted } from "vue"
133133
import { LazyStore } from "@tauri-apps/plugin-store"
134134
import { load } from "@hoppscotch/plugin-appload"
135135
import { getVersion } from "@tauri-apps/api/app"
@@ -183,6 +183,8 @@ const appVersion = ref("...")
183183
184184
const updaterService = new UpdaterService(appStore)
185185
186+
let progressPollingInterval: ReturnType<typeof setInterval> | undefined
187+
186188
const formatBytes = (bytes: number): string => {
187189
if (bytes === 0) return "0 Bytes"
188190
@@ -211,29 +213,54 @@ const setupUpdateStateWatcher = async () => {
211213
updateStatus.value = newValue.status
212214
updateMessage.value = newValue.message || ""
213215
214-
if (newValue.progress) {
215-
downloadProgress.value = newValue.progress
216-
}
217-
218216
if (newValue.status === UpdateStatus.AVAILABLE) {
219217
appState.value = AppState.UPDATE_AVAILABLE
220218
} else if (newValue.status === UpdateStatus.ERROR) {
221219
error.value = newValue.message || "Unknown error"
222220
appState.value = AppState.ERROR
221+
// Stop progress polling on error
222+
stopProgressPolling()
223223
} else if (
224224
newValue.status === UpdateStatus.DOWNLOADING ||
225225
newValue.status === UpdateStatus.INSTALLING
226226
) {
227227
appState.value = AppState.UPDATE_IN_PROGRESS
228+
// Start progress polling when downloading
229+
if (newValue.status === UpdateStatus.DOWNLOADING) {
230+
startProgressPolling()
231+
} else {
232+
// Stop progress polling when installing
233+
stopProgressPolling()
234+
}
228235
} else if (newValue.status === UpdateStatus.READY_TO_RESTART) {
229236
appState.value = AppState.UPDATE_READY
237+
// Stop progress polling when ready to restart
238+
stopProgressPolling()
230239
}
231240
}
232241
)
233242
234243
return unsubscribe
235244
}
236245
246+
const startProgressPolling = () => {
247+
if (progressPollingInterval) return
248+
249+
progressPollingInterval = setInterval(() => {
250+
const currentProgress = updaterService.getCurrentProgress()
251+
if (currentProgress.downloaded > downloadProgress.value.downloaded) {
252+
downloadProgress.value = currentProgress
253+
}
254+
}, 100)
255+
}
256+
257+
const stopProgressPolling = () => {
258+
if (progressPollingInterval) {
259+
clearInterval(progressPollingInterval)
260+
progressPollingInterval = undefined
261+
}
262+
}
263+
237264
const installUpdate = async () => {
238265
try {
239266
appState.value = AppState.UPDATE_IN_PROGRESS
@@ -244,6 +271,7 @@ const installUpdate = async () => {
244271
const errorMessage = err instanceof Error ? err.message : String(err)
245272
error.value = `Failed to install update: ${errorMessage}`
246273
appState.value = AppState.ERROR
274+
stopProgressPolling()
247275
}
248276
}
249277
@@ -332,6 +360,7 @@ const initialize = async () => {
332360
appState.value = AppState.LOADING
333361
error.value = ""
334362
downloadProgress.value = { downloaded: 0 }
363+
stopProgressPolling()
335364
336365
try {
337366
try {
@@ -368,4 +397,8 @@ const initialize = async () => {
368397
onMounted(() => {
369398
initialize()
370399
})
400+
401+
onUnmounted(() => {
402+
stopProgressPolling()
403+
})
371404
</script>

0 commit comments

Comments
 (0)