Skip to content

Commit

Permalink
fix: 修复 Windows 无法修改数据存储路径的问题 (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored Jan 6, 2025
1 parent 4ab4108 commit 77eb3f8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
11 changes: 4 additions & 7 deletions src/database/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { TableName, TablePayload } from "@/types/database";
import { emit } from "@tauri-apps/api/event";
import { remove } from "@tauri-apps/plugin-fs";
import Database from "@tauri-apps/plugin-sql";
import { entries, isBoolean, isNil, map, omitBy, some } from "lodash-es";

let db: Database | null;
let db: Database | null = null;

/**
* 初始化数据库
*/
export const initDatabase = async () => {
if (db) return;

const path = await getSaveDatabasePath();

db = await Database.load(`sqlite:${path}`);
Expand Down Expand Up @@ -62,9 +63,7 @@ const handlePayload = (payload: TablePayload) => {
* @param sql sql 语句
*/
export const executeSQL = async (query: string, values?: unknown[]) => {
if (!db) {
await initDatabase();
}
await initDatabase();

if (query.startsWith("SELECT") || query.startsWith("PRAGMA")) {
return await db!.select(query, values);
Expand Down Expand Up @@ -167,8 +166,6 @@ export const closeDatabase = async () => {
await db.close();

db = null;

emit(LISTEN_KEY.CLOSE_DATABASE);
};

/**
Expand Down
4 changes: 3 additions & 1 deletion src/pages/Backup/components/Manual/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ const Manual: FC<{ state: State }> = (props) => {

state.spinning = true;

await closeDatabase();
emit(LISTEN_KEY.CLOSE_DATABASE);

await wait();

await decompress(path, getSaveDataPath());

Expand Down
4 changes: 3 additions & 1 deletion src/pages/Backup/components/SavePath/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ const SavePath: FC<{ state: State }> = (props) => {

state.spinning = true;

await closeDatabase();
emit(LISTEN_KEY.CLOSE_DATABASE);

await wait();

await transfer(getSaveDataPath(), dstPath, {
includes: [
Expand Down
8 changes: 8 additions & 0 deletions src/utils/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 返回一个延迟指定时间后解决的 Promise,用于异步操作中的延时控制。
*
* @param 等待的时间,单位为毫秒,默认为 1000 毫秒。
*/
export const wait = (ms = 1000) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

0 comments on commit 77eb3f8

Please sign in to comment.