Tofu/app/services/planning/createMemoryPlanningStore.js
2026-09-18 01:41:02 +02:00

31 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// In-memory planning storage, used when the user continues without Nextcloud.
//
// Same contract as createPlanningStore (loadWeek / saveWeek / describeStorage)
// on purpose: the page receives a store and never has to test
// `client === null`. Two implementations, one contract — that is what makes
// this brick replaceable. Nothing survives a reload, and the label says so.
export function createMemoryPlanningStore() {
// The weeks are the mutable state of this store; only its own methods
// touch them.
const weeksById = new Map()
async function loadWeek(weekId) {
const planning = weeksById.get(weekId)
// Same shape as the remote store: an unknown week is an empty week.
return { ok: true, planning: planning === undefined ? null : planning }
}
async function saveWeek(weekId, planning) {
// Cloned so the stored value cannot be mutated from the outside, exactly
// like the remote store which serializes through JSON.
weeksById.set(weekId, structuredClone(planning))
return { ok: true }
}
function describeStorage() {
return { kind: 'memory', label: 'Hors ligne — rien nest enregistré' }
}
return { loadWeek, saveWeek, describeStorage }
}