Fix race condition when deleting multiple faults
This commit is contained in:
1 parent
756912eeb1
commit
2b61c8f7b8
1 file changed
+16
-10
+16
-10
@@ -46,6 +46,18 @@ const unpackSmartQuery = memoize((query) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function deleteFaults(faults: Iterable<string>): Promise<void> {
|
||||||
|
const proms: Map<string, Promise<void>> = new Map();
|
||||||
|
for (const f of faults) {
|
||||||
|
const deviceId = f.split(":", 1)[0];
|
||||||
|
let p = proms.get(deviceId);
|
||||||
|
if (p == null) p = store.deleteResource("faults", f);
|
||||||
|
else p = p.then(() => store.deleteResource("faults", f));
|
||||||
|
proms.set(deviceId, p);
|
||||||
|
}
|
||||||
|
await Promise.all(proms.values());
|
||||||
|
}
|
||||||
|
|
||||||
export function init(
|
export function init(
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
): Promise<Record<string, unknown>> {
|
): Promise<Record<string, unknown>> {
|
||||||
@@ -154,16 +166,10 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
if (!confirm(`Deleting ${selected.size} faults. Are you sure?`))
|
if (!confirm(`Deleting ${selected.size} faults. Are you sure?`))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Promise.all(
|
const c = selected.size;
|
||||||
Array.from(selected).map((id) =>
|
deleteFaults(selected)
|
||||||
store.deleteResource("faults", id),
|
.then(() => {
|
||||||
),
|
notifications.push("success", `${c} faults deleted`);
|
||||||
)
|
|
||||||
.then((res) => {
|
|
||||||
notifications.push(
|
|
||||||
"success",
|
|
||||||
`${res.length} faults deleted`,
|
|
||||||
);
|
|
||||||
store.setTimestamp(Date.now());
|
store.setTimestamp(Date.now());
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
|||||||
Reference in new issue
Block a user