From bd8a249b3c6c78fae5ed978c0181664f8ed29fc6 Mon Sep 17 00:00:00 2001 From: Huakun Shen Date: Thu, 3 Apr 2025 11:20:07 -0400 Subject: [PATCH] Enhance clipboard and database management in initialization process - Added logging to `cleanClipboard` to indicate the number of clipboard entries older than a specified number of days. - Introduced a new utility function `vacuumSqlite` for database maintenance, which is now called during app initialization. - Updated the `init` function to await the completion of `cleanClipboard` and `vacuumSqlite` for better error handling and flow control. - Ensured that the console attachment in `onMount` is awaited for proper synchronization. --- apps/desktop/src/lib/utils/clipboard.ts | 1 + apps/desktop/src/lib/utils/db.ts | 13 +++++++++++++ apps/desktop/src/lib/utils/init.ts | 6 ++++-- apps/desktop/src/routes/app/+layout.svelte | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 apps/desktop/src/lib/utils/db.ts diff --git a/apps/desktop/src/lib/utils/clipboard.ts b/apps/desktop/src/lib/utils/clipboard.ts index 61ec246..a1b604e 100644 --- a/apps/desktop/src/lib/utils/clipboard.ts +++ b/apps/desktop/src/lib/utils/clipboard.ts @@ -24,6 +24,7 @@ export async function cleanClipboard() { ) ) const nLinesToDelete = oldClipboardData.at(0)?.count ?? 0 + console.info(`Found ${nLinesToDelete} clipboard entries older than ${nDays} days to clean up`) info(`Found ${nLinesToDelete} clipboard entries older than ${nDays} days to clean up`) // Now delete the old data diff --git a/apps/desktop/src/lib/utils/db.ts b/apps/desktop/src/lib/utils/db.ts new file mode 100644 index 0000000..44c46d2 --- /dev/null +++ b/apps/desktop/src/lib/utils/db.ts @@ -0,0 +1,13 @@ +import { proxyDB } from "@kksh/drizzle" +import { error, info } from "@tauri-apps/plugin-log" +import { sql } from "drizzle-orm" + +export async function vacuumSqlite() { + const statement = sql`VACUUM` + try { + await proxyDB.run(statement) + info("Vacuumed sqlite") + } catch (error) { + console.error(`Failed to vacuum sqlite: ${error}`) + } +} diff --git a/apps/desktop/src/lib/utils/init.ts b/apps/desktop/src/lib/utils/init.ts index 59731cb..44327e2 100644 --- a/apps/desktop/src/lib/utils/init.ts +++ b/apps/desktop/src/lib/utils/init.ts @@ -3,13 +3,14 @@ import { getCurrentWindow } from "@tauri-apps/api/window" import { error, info } from "@tauri-apps/plugin-log" import { dev } from "$app/environment" import { cleanClipboard } from "./clipboard" +import { vacuumSqlite } from "./db" import { mapKeyToTauriKey, registerAppHotkey } from "./hotkey" import { listenToReloadOneExtension } from "./tauri-events" /** * Initialize the app */ -export function init() { +export async function init() { const window = getCurrentWindow() if (window.label === "main") { initMainWindow() @@ -18,13 +19,14 @@ export function init() { extensions.reloadExtension(extPath) }) } - cleanClipboard() + await cleanClipboard() .then(() => { info("Cleaned clipboard") }) .catch((e) => { error(`Failed to clean clipboard: ${e}`) }) + vacuumSqlite() if (!dev) { // document.addEventListener("contextmenu", function (event) { // event.preventDefault() diff --git a/apps/desktop/src/routes/app/+layout.svelte b/apps/desktop/src/routes/app/+layout.svelte index 51457ea..1616f5e 100644 --- a/apps/desktop/src/routes/app/+layout.svelte +++ b/apps/desktop/src/routes/app/+layout.svelte @@ -53,7 +53,7 @@ unlisteners.forEach((unlistener) => unlistener()) }) onMount(async () => { - attachConsole().then((unlistener) => unlisteners.push(unlistener)) + await attachConsole().then((unlistener) => unlisteners.push(unlistener)) initDeeplink().then((unlistener) => unlisteners.push(unlistener)) shellx .fixPathEnv()