2026-10-04 20:19:03 +01:00
|
|
|
(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, () => ({
|
2026-10-06 10:07:21 +01:00
|
|
|
mergeSnapshot(localChecks, savingIds, currentRevision, snapshot, localPlantings = {}, savingPlantings = new Set()) {
|
2026-10-04 20:19:03 +01:00
|
|
|
if (!snapshot || !Number.isInteger(snapshot.revision) || snapshot.revision <= currentRevision) {
|
2026-10-06 10:07:21 +01:00
|
|
|
return { applied: false, revision: currentRevision, checks: localChecks, plantings: localPlantings };
|
2026-10-04 20:19:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const checks = { ...(snapshot.checks || {}) };
|
|
|
|
|
for (const itemId of savingIds) {
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(localChecks, itemId)) {
|
|
|
|
|
checks[itemId] = Boolean(localChecks[itemId]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-06 10:07:21 +01:00
|
|
|
const plantings = { ...(snapshot.plantings || {}) };
|
|
|
|
|
for (const itemId of savingPlantings) {
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(localPlantings, itemId)) plantings[itemId] = localPlantings[itemId];
|
|
|
|
|
else delete plantings[itemId];
|
|
|
|
|
}
|
|
|
|
|
return { applied: true, revision: snapshot.revision, checks, plantings };
|
2026-10-04 20:19:03 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
confirmWrite(localChecks, currentRevision, result) {
|
|
|
|
|
return {
|
|
|
|
|
checks: { ...localChecks, [result.itemId]: Boolean(result.checked) },
|
|
|
|
|
revision: Math.max(currentRevision, result.revision)
|
|
|
|
|
};
|
2026-10-06 10:07:21 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
confirmPlanting(localPlantings, currentRevision, result) {
|
|
|
|
|
const plantings = { ...localPlantings };
|
|
|
|
|
if (result.planted) plantings[result.itemId] = result.planted;
|
|
|
|
|
else delete plantings[result.itemId];
|
|
|
|
|
return { plantings, revision: Math.max(currentRevision, result.revision) };
|
2026-10-04 20:19:03 +01:00
|
|
|
}
|
|
|
|
|
}));
|