feat: Doing more polishing (M8b)
This commit is contained in:
+12
-1
@@ -46,6 +46,13 @@ export function encodeSave(state) {
|
||||
export const MIGRATIONS = {
|
||||
// v2 added the world cycle: older saves start it at neutral.
|
||||
1: (s) => ({ ...s, version: 2, world: { cycle: 0 } }),
|
||||
// v3 replaced the single pending card by the in-tray queue.
|
||||
2: ({ pendingCard, ...s }) => ({
|
||||
...s,
|
||||
version: 3,
|
||||
inbox: pendingCard ? [{ id: pendingCard.id, day: pendingCard.day }] : [],
|
||||
pending: { ...s.pending, weeklyCard: Boolean(s.pending?.weeklyCard) && !pendingCard, inboxFull: false },
|
||||
}),
|
||||
};
|
||||
|
||||
export function migrate(state) {
|
||||
@@ -55,7 +62,11 @@ export function migrate(state) {
|
||||
while (s.version < STATE_VERSION) {
|
||||
const step = MIGRATIONS[s.version];
|
||||
if (!step) return { ok: false, error: "version" };
|
||||
s = step(s);
|
||||
try {
|
||||
s = step(s);
|
||||
} catch {
|
||||
return { ok: false, error: "format" }; // a damaged or hostile save must never throw
|
||||
}
|
||||
}
|
||||
return { ok: true, state: s };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user