From 1a8bb64ff268b03a9108a2e2467987f8ae001115 Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Mon, 7 Aug 2023 10:49:01 +0200 Subject: [PATCH] small refactoring --- src/lib/core/refactor/Composer.svelte | 70 +++++++++++++-------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/src/lib/core/refactor/Composer.svelte b/src/lib/core/refactor/Composer.svelte index c1b5674..052cc9d 100644 --- a/src/lib/core/refactor/Composer.svelte +++ b/src/lib/core/refactor/Composer.svelte @@ -17,13 +17,12 @@ } interface IComposer { - layout?: IComposerLayout; id: string; - me?: { id: string; do: any; data: any }; - slot?: string; + layout?: IComposerLayout; component?: string; - data?: Record; + slot?: string; children?: IComposer[]; + data?: Record; machine?: any; } @@ -35,11 +34,11 @@ $: { layoutStyle = computeLayoutStyle(composer?.layout); - initializeComposerState(composer); + loadComponentAndInitializeState(composer); if (composer?.children) { composer.children.forEach((child) => { - initializeComposerState(child); + loadComponentAndInitializeState(child); }); } @@ -52,6 +51,32 @@ } } + async function loadComponentAndInitializeState(component: IComposer) { + if (!component) return; + + if (component.id) { + component.store = createComposerStore(component.id, component.store || {}); + if (component.data?.map) { + subscribeAndMapQueries(component.id, component.data.map); + } + } + + if (component.children) { + component.children.forEach(loadComponentAndInitializeState); + } + + // Load component and service + const componentName = component.component || 'FallBack'; + component.me = { + id: component.id, + do: { + core: coreServices, + machine: component.machineService || null + } + }; + return await getComponent(componentName); + } + function initializeAndStartMachine(composer: IComposer) { if (composer?.machine) { const machine = Machine( @@ -85,21 +110,6 @@ `; } - function initializeComposerState(child: IComposer) { - if (!child) return; - - if (child.id) { - child.store = createComposerStore(child.id, child.store || {}); - if (child.data?.map) { - subscribeAndMapQueries(child.id, child.data.map); - } - } - - if (child.children) { - child.children.forEach(initializeComposerState); - } - } - onDestroy(() => { unsubscribers.forEach((unsub) => unsub()); }); @@ -111,30 +121,17 @@ } return FallBack; } - - async function loadComponentAndService(component: IComposer) { - const componentName = component.component || 'FallBack'; - component.me = { - id: component.id, - do: { - core: coreServices, - machine: component.machineService || null - } - }; - return await getComponent(componentName); - }
- {#await loadComponentAndService(composer) then Component} + {#await loadComponentAndInitializeState(composer) then Component} {/await} @@ -144,12 +141,11 @@ class="grid w-full h-full overflow-hidden ${composer?.layout?.style || ''}" style={`grid-area: ${child.slot}`} > - {#await loadComponentAndService(child) then ChildComponent} + {#await loadComponentAndInitializeState(child) then ChildComponent} {#if child.children && child.children.length}