From 00cea8429c157e8526c946bbf00045e4394dcf25 Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Fri, 4 Aug 2023 18:34:39 +0200 Subject: [PATCH] abstracting generic queryStoreMapping --- .../components/refactor/ComposerWrap.svelte | 27 +++++-------------- src/lib/core/refactor/Composer.svelte | 15 ++++++++++- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/lib/components/refactor/ComposerWrap.svelte b/src/lib/components/refactor/ComposerWrap.svelte index 7144b8d..5b1f807 100644 --- a/src/lib/components/refactor/ComposerWrap.svelte +++ b/src/lib/components/refactor/ComposerWrap.svelte @@ -30,31 +30,16 @@ id: 'ComposerCharly', component: 'ComposerCharly', slot: 'aside', - // data: { - // queries: { - // todos: queryTodos, - // messages: queryMessages - // } - // }, + data: { + queries: { + todos: queryTodos, + messages: queryMessages + } + }, machine: { initial: 'LOADING', states: { LOADING: { - entry: () => { - function subscribeAndMapQueries(queryMap) { - Object.entries(queryMap).forEach(([name, query]) => { - query.subscribe((value) => { - if (value) { - coreServices.mutateStore('ComposerCharly', { [name]: value }); - } - }); - }); - } - return subscribeAndMapQueries({ - todos: queryTodos, - messages: queryMessages - }); - }, on: { TOGGLE: { target: 'READY' diff --git a/src/lib/core/refactor/Composer.svelte b/src/lib/core/refactor/Composer.svelte index 52af13b..5352a9e 100644 --- a/src/lib/core/refactor/Composer.svelte +++ b/src/lib/core/refactor/Composer.svelte @@ -90,6 +90,16 @@ } } + function subscribeAndMapQueries(id: string, queryMap: Record) { + Object.entries(queryMap).forEach(([name, query]) => { + query.subscribe((value) => { + if (value) { + coreServices.mutateStore(id, { [name]: value }); + } + }); + }); + } + function computeLayoutStyle(layout?: IComposerLayout): string { if (!layout) return ''; return ` @@ -101,10 +111,13 @@ } function initializeComposerState(child: IComposer) { - if (!child) return; // Add this line + if (!child) return; if (child.id) { child.store = createComposerStore(child.id, child.store || {}); + if (child.data?.queries) { + subscribeAndMapQueries(child.id, child.data.queries); + } } if (child.children) {