From 09ce4bd6725bf1c668ab4b8253e9d76f0eae3e97 Mon Sep 17 00:00:00 2001 From: Miodec Date: Thu, 19 Sep 2024 14:07:35 +0200 Subject: [PATCH] impr(local storage with schema): add fallback value copy to migrate parameters !nuf --- frontend/src/ts/utils/local-storage-with-schema.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/frontend/src/ts/utils/local-storage-with-schema.ts b/frontend/src/ts/utils/local-storage-with-schema.ts index be26f3458..42c3d593b 100644 --- a/frontend/src/ts/utils/local-storage-with-schema.ts +++ b/frontend/src/ts/utils/local-storage-with-schema.ts @@ -1,16 +1,17 @@ import { ZodError, ZodIssue } from "zod"; +import { deepClone } from "./misc"; export class LocalStorageWithSchema { private key: string; private schema: Zod.Schema; private fallback: T; - private migrate?: (value: unknown, zodIssues: ZodIssue[]) => T; + private migrate?: (value: unknown, zodIssues: ZodIssue[], fallback: T) => T; constructor(options: { key: string; schema: Zod.Schema; fallback: T; - migrate?: (value: unknown, zodIssues: ZodIssue[]) => T; + migrate?: (value: unknown, zodIssues: ZodIssue[], fallback: T) => T; }) { this.key = options.key; this.schema = options.schema; @@ -50,7 +51,11 @@ export class LocalStorageWithSchema { let newValue = this.fallback; if (this.migrate) { - const migrated = this.migrate(jsonParsed, schemaParsed.error.issues); + const migrated = this.migrate( + jsonParsed, + schemaParsed.error.issues, + deepClone(this.fallback) + ); const parse = this.schema.safeParse(migrated); if (parse.success) { newValue = migrated;