abstracting generic queryStoreMapping

This commit is contained in:
Samuel Andert 2023-08-04 18:34:39 +02:00
parent 9bb780cd96
commit 00cea8429c
2 changed files with 20 additions and 22 deletions

View File

@ -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'

View File

@ -90,6 +90,16 @@
}
}
function subscribeAndMapQueries(id: string, queryMap: Record<string, any>) {
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) {