26 lines
1,016 B
JavaScript
26 lines
1,016 B
JavaScript
(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)
|
|
};
|
|
}
|
|
}));
|