Skip to content

Commit d4c3873

Browse files
committed
🐛 修复软件启动时不会读取设置
1 parent a045cf0 commit d4c3873

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

app/src/core/service/Settings.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LazyStore } from "@tauri-apps/plugin-store";
22
import { useEffect, useState } from "react";
3+
import { toast } from "sonner";
34
import z from "zod";
45

56
export const settingsSchema = z.object({
@@ -151,10 +152,32 @@ export const settingsSchema = z.object({
151152

152153
export type Settings = z.infer<typeof settingsSchema>;
153154

155+
const listeners: Partial<Record<string, ((value: any) => void)[]>> = {};
156+
154157
const store = new LazyStore("settings.json");
155158
await 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

159182
export 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

Comments
 (0)