Fix concurrent checklist sync and consistent actions

This commit is contained in:
Codex 2026-10-04 20:19:03 +01:00
parent a33f7a297d
commit 8d5a94d7fa
10 changed files with 124 additions and 20 deletions

26
static/sync.js Normal file
View file

@ -0,0 +1,26 @@
(function exposeSync(root, factory) {
const api = factory();
if (typeof module === "object" && module.exports) module.exports = api;
if (root) root.ValleySync = api;
})(typeof window === "undefined" ? globalThis : window, () => ({
mergeSnapshot(localChecks, savingIds, currentRevision, snapshot) {
if (!snapshot || !Number.isInteger(snapshot.revision) || snapshot.revision <= currentRevision) {
return { applied: false, revision: currentRevision, checks: localChecks };
}
const checks = { ...(snapshot.checks || {}) };
for (const itemId of savingIds) {
if (Object.prototype.hasOwnProperty.call(localChecks, itemId)) {
checks[itemId] = Boolean(localChecks[itemId]);
}
}
return { applied: true, revision: snapshot.revision, checks };
},
confirmWrite(localChecks, currentRevision, result) {
return {
checks: { ...localChecks, [result.itemId]: Boolean(result.checked) },
revision: Math.max(currentRevision, result.revision)
};
}
}));