stardew-tracker/static/sync.js

39 lines
1.6 KiB
JavaScript
Raw Normal View History

(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, localPlantings = {}, savingPlantings = new Set()) {
if (!snapshot || !Number.isInteger(snapshot.revision) || snapshot.revision <= currentRevision) {
return { applied: false, revision: currentRevision, checks: localChecks, plantings: localPlantings };
}
const checks = { ...(snapshot.checks || {}) };
for (const itemId of savingIds) {
if (Object.prototype.hasOwnProperty.call(localChecks, itemId)) {
checks[itemId] = Boolean(localChecks[itemId]);
}
}
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 };
},
confirmWrite(localChecks, currentRevision, result) {
return {
checks: { ...localChecks, [result.itemId]: Boolean(result.checked) },
revision: Math.max(currentRevision, result.revision)
};
},
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) };
}
}));