11import { LazyStore } from "@tauri-apps/plugin-store" ;
22import { useEffect , useState } from "react" ;
3+ import { toast } from "sonner" ;
34import z from "zod" ;
45
56export const settingsSchema = z . object ( {
@@ -151,10 +152,32 @@ export const settingsSchema = z.object({
151152
152153export type Settings = z . infer < typeof settingsSchema > ;
153154
155+ const listeners : Partial < Record < string , ( ( value : any ) => void ) [ ] > > = { } ;
156+
154157const store = new LazyStore ( "settings.json" ) ;
155158await store . init ( ) ;
156159
157- const listeners : Record < string , ( ( value : any ) => void ) [ ] > = { } ;
160+ // store加载完成后,推送所有listeners初始值
161+ // for (const key in listeners) {
162+ // if (Object.prototype.hasOwnProperty.call(listeners, key)) {
163+ // // 取store中的值,如果没有则用默认值
164+ // let value = await store.get(key);
165+ // if (value === undefined) {
166+ // value = settingsSchema._def.shape()[key as keyof Settings]._def.defaultValue();
167+ // }
168+ // listeners[key]?.forEach((cb) => cb(value));
169+ // }
170+ // }
171+ let savedSettings = settingsSchema . parse ( { } ) ;
172+ try {
173+ console . log ( Object . fromEntries ( await store . entries ( ) ) ) ;
174+ savedSettings = settingsSchema . parse ( Object . fromEntries ( await store . entries ( ) ) ) ;
175+ } catch ( e ) {
176+ if ( e instanceof z . ZodError ) {
177+ console . error ( e ) ;
178+ toast . error ( `设置文件格式错误\n${ JSON . stringify ( e . issues ) } ` ) ;
179+ }
180+ }
158181
159182export const Settings = new Proxy <
160183 Settings & {
@@ -163,7 +186,7 @@ export const Settings = new Proxy<
163186 }
164187> (
165188 {
166- ...settingsSchema . parse ( { } ) ,
189+ ...savedSettings ,
167190 watch : ( ) => ( ) => { } ,
168191 use : ( ) => [ undefined as any , ( ) => { } ] ,
169192 } ,
0 commit comments