Stop orphan game links from blocking global imports
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 20m13s
Validate and deploy Sokko G / deploy (push) Successful in 11s

A game link could outlive its toolbox when one tab linked a game while
another tab, unaware of that link, deleted the toolbox. Auto-backups and
manual exports then copied the orphan link, and the global import
rejected the whole file.

- Drop links to missing toolboxes inside the IndexedDB transaction.
- Remove orphan links already stored when the page loads.
- Ignore a link request to a toolbox that no longer exists.
- Leave orphan links out of exports and skip them on global import, so
  older backups import again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-11 15:12:34 +01:00
parent 98c442a9cd
commit d030c3f59f
9 changed files with 137 additions and 9 deletions

View file

@ -232,6 +232,39 @@ function applyToolboxChanges(current, mutation, conflicts) {
return [...remote, ...ordered];
}
function keepLinksToToolboxes(links, toolboxIds) {
return Object.fromEntries(Object.entries(links || {})
.filter(([, toolboxId]) => typeof toolboxId === "string" && toolboxIds.has(toolboxId)));
}
// Retire du stockage les associations de jeu dont la toolbox n'existe plus.
export async function removeOrphanLinks() {
const db = await openDatabase();
let removed = 0;
await new Promise((resolve, reject) => {
const transaction = db.transaction(KV_STORE, "readwrite");
const kv = transaction.objectStore(KV_STORE);
const toolboxRequest = kv.get("toolboxes");
const linksRequest = kv.get("links");
let remaining = 2;
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
transaction.onabort = () => reject(transaction.error);
function cleanWhenReady() {
remaining -= 1;
if (remaining) return;
const links = linksRequest.result && typeof linksRequest.result === "object" ? linksRequest.result : {};
const toolboxIds = new Set((Array.isArray(toolboxRequest.result) ? toolboxRequest.result : []).map((toolbox) => toolbox?.id));
const kept = keepLinksToToolboxes(links, toolboxIds);
removed = Object.keys(links).length - Object.keys(kept).length;
if (removed) kv.put(kept, "links");
}
toolboxRequest.onsuccess = cleanWhenReady;
linksRequest.onsuccess = cleanWhenReady;
});
return removed;
}
export async function mutateToolboxStorage(mutation, { normalizeToolboxes = (value) => value } = {}) {
if (!mutation?.changed) return;
const db = await openDatabase();
@ -291,7 +324,9 @@ export async function mutateToolboxStorage(mutation, { normalizeToolboxes = (val
}
if (mutation.toolboxes.changes.length || mutation.toolboxes.order) kv.put(nextToolboxes, "toolboxes");
if (mutation.links.length) kv.put(nextLinks, "links");
// Une toolbox supprimée emporte aussi les associations écrites par un autre onglet.
const keptLinks = keepLinksToToolboxes(nextLinks, validToolboxIds);
if (mutation.links.length || !storageValuesEqual(keptLinks, currentLinks)) kv.put(keptLinks, "links");
for (const change of mutation.modules) {
if (change.nextExists) modules.put({ key: change.key, data: change.nextValue });
else modules.delete(change.key);