From 6a9c7b82ccb17d6fa8aa4065628068ad1bd35c07 Mon Sep 17 00:00:00 2001 From: fabianvanrissenbeck <fabian.vanrissenbeck@tu-dortmund.de> Date: Thu, 4 Apr 2024 15:27:27 +0200 Subject: [PATCH] LayoutService: periodic config reload --- src/services/LayoutService.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/services/LayoutService.ts b/src/services/LayoutService.ts index 2b90cad..1d7a6f1 100644 --- a/src/services/LayoutService.ts +++ b/src/services/LayoutService.ts @@ -13,11 +13,14 @@ export class LayoutService { static async init(): Promise<void> { try { - const activeConfigs = await fetch("/activeConfigs.json").then(content => content.json()); - const configFetches = (activeConfigs as string[]) - .map(configPath => fetch(configPath).then(content => content.json())); - - LayoutService.configs = await Promise.all(configFetches); + LayoutService.configs = await fetchActiveConfigs(); + const autoRefreshInterval = 1000 * 60 * 10; + + setInterval(() => { + fetchActiveConfigs() + .then(cfg => this.configs = cfg) + .catch(_ => console.log("cannot refresh layout config")); + }, autoRefreshInterval); } catch (e) { console.error("LayoutService could not init", e) } @@ -37,10 +40,17 @@ export class LayoutService { }, false); }); - console.log(activeConfigs) - - /* ToDo: This is not great, as it assumes there is always an active layout. If you don't configure this correctly, - consider yourself warned now and don't blame me */ - return activeConfigs.at(0) ?? NO_LAYOUT_CONFIG; + const defaultConfig = this.configs.filter(config => config.id === "default").at(0); + return activeConfigs.at(0) ?? defaultConfig ?? NO_LAYOUT_CONFIG; } } + +async function fetchActiveConfigs(): Promise<LayoutConfig[]> { + const activeConfigs = await fetch("/activeConfigs.json").then(content => content.json()); + + const configFetches = (activeConfigs as string[]) + .map(configPath => fetch(configPath).then(content => content.json())); + + const configs: LayoutConfig[] = (await Promise.all(configFetches)).map(item => item as LayoutConfig); + return configs; +} -- GitLab