From 2818700182af52cdfff893f0983be6d2411f8d6c Mon Sep 17 00:00:00 2001 From: josc146 Date: Wed, 27 Mar 2024 10:19:35 +0800 Subject: [PATCH] avoid program lag caused by frequent triggering of read/write operations due to Linux file system notification --- frontend/src/startup.ts | 10 +++++++--- frontend/src/utils/index.tsx | 26 ++++++++++++++------------ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/frontend/src/startup.ts b/frontend/src/startup.ts index 5ecd970..deb723b 100644 --- a/frontend/src/startup.ts +++ b/frontend/src/startup.ts @@ -9,6 +9,7 @@ import { t } from 'i18next'; import { Preset } from './types/presets'; import { toast } from 'react-toastify'; import { MidiMessage, MidiPort } from './types/composition'; +import { throttle } from 'lodash-es'; export async function startup() { initPresets(); @@ -103,7 +104,7 @@ async function initPresets() { } async function initLoraModels() { - const refreshLoraModels = () => { + const refreshLoraModels = throttle(() => { ListDirFiles('lora-models').then((data) => { if (!data) return; const loraModels = []; @@ -114,7 +115,7 @@ async function initLoraModels() { } commonStore.setLoraModels(loraModels); }); - }; + }, 2000); refreshLoraModels(); EventsOn('fsnotify', (data: string) => { @@ -124,9 +125,12 @@ async function initLoraModels() { } async function initLocalModelsNotify() { + const throttleRefreshLocalModels = throttle(() => { + refreshLocalModels({ models: commonStore.modelSourceList }, false); //TODO fix bug that only add models + }, 2000); EventsOn('fsnotify', (data: string) => { if (data.includes('models') && !data.includes('lora-models')) - refreshLocalModels({ models: commonStore.modelSourceList }, false); //TODO fix bug that only add models + throttleRefreshLocalModels(); }); } diff --git a/frontend/src/utils/index.tsx b/frontend/src/utils/index.tsx index 27a5ed8..71294b6 100644 --- a/frontend/src/utils/index.tsx +++ b/frontend/src/utils/index.tsx @@ -61,8 +61,7 @@ export async function refreshBuiltInModels(readCache: boolean = false) { else cache.models = manifest.models.slice(); commonStore.setModelSourceList(cache.models); - await saveCache().catch(() => { - }); + saveCache(); return cache; } @@ -120,8 +119,7 @@ export async function refreshLocalModels(cache: { commonStore.setModelSourceList(cache.models); if (initUnfinishedModels) initLastUnfinishedModelDownloads(); - await saveCache().catch(() => { - }); + saveCache(); } function initLastUnfinishedModelDownloads() { @@ -240,14 +238,18 @@ export const saveConfigs = throttle(async () => { trailing: true }); -export const saveCache = async () => { - const data: Cache = { - version: manifest.version, - models: commonStore.modelSourceList, - depComplete: commonStore.depComplete - }; - return SaveJson('cache.json', data); -}; +export const saveCache = throttle(async () => { + const data: Cache = { + version: manifest.version, + models: commonStore.modelSourceList, + depComplete: commonStore.depComplete + }; + return SaveJson('cache.json', data); + }, 1000, + { + leading: true, + trailing: true + }); export const savePresets = async () => { return SaveJson('presets.json', commonStore.presets);