Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit f0e1af6

Browse files
authored
prevent uninstall multiple click (#567)
1 parent c50a005 commit f0e1af6

3 files changed

Lines changed: 51 additions & 21 deletions

File tree

modules/desktop/src/components/package-banner/package-banner.svelte

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,54 @@
1919
2020
export let pkg: GUIPackage;
2121
let installing = false;
22-
let pruning = false;
22+
let uninstalling = false;
2323
2424
const install = async (version: string) => {
25-
installing = true;
26-
await packagesStore.installPkg(pkg, version);
27-
installing = false;
25+
if (installing) {
26+
return;
27+
}
28+
try {
29+
installing = true;
30+
await packagesStore.installPkg(pkg, version);
31+
} finally {
32+
installing = false;
33+
}
34+
};
35+
36+
const uninstall = async () => {
37+
if (uninstalling) {
38+
return;
39+
}
40+
41+
try {
42+
uninstalling = true;
43+
await packagesStore.uninstallPkg(pkg);
44+
} finally {
45+
uninstalling = false;
46+
}
2847
};
2948
3049
const prune = async () => {
31-
pruning = true;
32-
const versions = (pkg?.installed_versions || []).sort((a, b) => semverCompare(b, a));
33-
for (const [i, v] of versions.entries()) {
34-
if (i) {
35-
// skip the latest version = 0
36-
try {
37-
await packagesStore.deletePkg(pkg, v);
38-
} catch (e) {
39-
console.error(e);
50+
if (uninstalling) {
51+
return;
52+
}
53+
54+
try {
55+
uninstalling = true;
56+
const versions = (pkg?.installed_versions || []).sort((a, b) => semverCompare(b, a));
57+
for (const [i, v] of versions.entries()) {
58+
if (i) {
59+
// skip the latest version = 0
60+
try {
61+
await packagesStore.deletePkg(pkg, v);
62+
} catch (e) {
63+
console.error(e);
64+
}
4065
}
4166
}
67+
} finally {
68+
uninstalling = false;
4269
}
43-
pruning = false;
4470
};
4571
4672
let copied = false;
@@ -100,10 +126,8 @@
100126
class="h-10"
101127
type="plain"
102128
color="blue"
103-
onClick={async () => {
104-
packagesStore.uninstallPkg(pkg);
105-
}}
106-
loading={pruning}
129+
onClick={uninstall}
130+
loading={uninstalling}
107131
>
108132
<div class="version-item flex w-full items-center justify-center gap-x-1 text-xs">
109133
<div class="icon-trash" />
@@ -125,7 +149,7 @@
125149
type="plain"
126150
color="blue"
127151
onClick={prune}
128-
loading={pruning}
152+
loading={uninstalling}
129153
>
130154
<div class="version-item flex w-full items-center justify-center gap-x-1 text-xs">
131155
<div class="icon-scissors" />

modules/desktop/src/libs/stores/pkgs.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import withRetry from "$libs/utils/retry";
2727

2828
import log from "$libs/logger";
2929
import { isPackageUpToDate } from "../packages/pkg-utils";
30+
import withDelay from "$libs/utils/delay";
3031

3132
const packageRefreshInterval = 1000 * 60 * 60; // 1 hour
3233

@@ -254,11 +255,11 @@ To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}).
254255
await deletePkg(pkg, v);
255256
}
256257

257-
setTimeout(() => {
258+
await withDelay(() => {
258259
updatePackage(pkg.full_name, {
259260
installed_versions: []
260261
});
261-
}, 3000);
262+
}, 1000);
262263
} catch (error) {
263264
log.error(error);
264265
notificationStore.add({
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// withDelay adds a delay before calling the provided function.
2+
export default async function withDelay<T>(f: () => T, delayMs: number) {
3+
await new Promise((resolve) => setTimeout(resolve, delayMs));
4+
return f();
5+
}

0 commit comments

Comments
 (0)