diff --git a/packages/api/src/controllers/config.js b/packages/api/src/controllers/config.js index 4935b337..f96877d5 100644 --- a/packages/api/src/controllers/config.js +++ b/packages/api/src/controllers/config.js @@ -59,13 +59,10 @@ module.exports = { getSettings_meta: true, async getSettings() { - try { - return this.fillMissingSettings( - JSON.parse(await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' })) - ); - } catch (err) { - return this.fillMissingSettings({}); - } + const res = await lock.acquire('settings', async () => { + return await this.loadSettings(); + }); + return res; }, fillMissingSettings(value) { @@ -79,12 +76,21 @@ module.exports = { return res; }, + async loadSettings() { + try { + const settingsText = await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' }); + return this.fillMissingSettings(JSON.parse(settingsText)); + } catch (err) { + return this.fillMissingSettings({}); + } + }, + updateSettings_meta: true, async updateSettings(values, req) { if (!hasPermission(`settings/change`, req)) return false; - const res = await lock.acquire('update', async () => { - const currentValue = await this.getSettings(); + const res = await lock.acquire('settings', async () => { + const currentValue = await this.loadSettings(); try { const updated = { ...currentValue, diff --git a/packages/web/src/stores.ts b/packages/web/src/stores.ts index 785ab080..4f24fe84 100644 --- a/packages/web/src/stores.ts +++ b/packages/web/src/stores.ts @@ -29,7 +29,11 @@ export function writableWithStorage(defaultValue: T, storageName) { } export function writableSettingsValue(defaultValue: T, storageName) { - const res = derived(useSettings(), $settings => ($settings || {})[storageName] ?? defaultValue); + const res = derived(useSettings(), $settings => { + const obj = $settings || {}; + // console.log('GET SETTINGS', $settings, storageName, obj[storageName]); + return obj[storageName] ?? defaultValue; + }); return { ...res, set: value => apiCall('config/update-settings', { [storageName]: value }), diff --git a/packages/web/src/utility/metadataLoaders.ts b/packages/web/src/utility/metadataLoaders.ts index f3428d3b..4d64ae9a 100644 --- a/packages/web/src/utility/metadataLoaders.ts +++ b/packages/web/src/utility/metadataLoaders.ts @@ -189,25 +189,29 @@ async function getCore(loader, args) { function useCore(loader, args) { const { url, params, reloadTrigger, transform, onLoaded } = loader(args); const cacheKey = stableStringify({ url, ...params }); - let closed = false; + let openedCount = 0; return { subscribe: onChange => { async function handleReload() { const res = await getCore(loader, args); - if (!closed) { + if (openedCount > 0) { onChange(res); } } - + openedCount += 1; handleReload(); if (reloadTrigger) { subscribeCacheChange(reloadTrigger, cacheKey, handleReload); return () => { - closed = true; + openedCount -= 1; unsubscribeCacheChange(reloadTrigger, cacheKey, handleReload); }; + } else { + return () => { + openedCount -= 1; + }; } }, };