31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
// 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 n’est enregistré' }
|
||
}
|
||
|
||
return { loadWeek, saveWeek, describeStorage }
|
||
}
|