added dynamic data loading and mapping towards components.

This commit is contained in:
Samuel Andert
2023-07-26 15:16:41 +02:00
parent 14aad071f4
commit 920e7b7ca1
6 changed files with 99 additions and 17 deletions

View File

@ -1,7 +1,9 @@
<script lang="ts">
import { onDestroy, onMount } from 'svelte';
import Composite from './Composite.svelte';
import components from '$lib/core/componentLoader';
import services from '$lib/core/servicesLoader';
import { dataStore } from '$lib/core/dataLoader';
import { createCompositeStore, getCompositeStore } from '$lib/core/compositeStores';
interface IComposite {
@ -34,6 +36,7 @@
`
: '';
// Reactive statement to watch changes in the composite prop
$: if (composite?.children) {
composite.children.forEach((child) => {
initializeCompositeState(child);
@ -59,21 +62,30 @@
for (const [localKey, external] of Object.entries(component.map)) {
const [externalID, externalKey] = external.split('.').map((item) => item.trim());
const externalStore = getCompositeStore(externalID);
if (externalStore) {
externalStore.subscribe((externalState) => {
console.log('External state:', externalState);
if (externalState && externalKey in externalState) {
if (externalID === 'data') {
const unsubscribe = dataStore.subscribe((store) => {
if (externalKey in store) {
localStore.update((storeValue) => {
storeValue = storeValue || {};
if (storeValue[localKey] !== externalState[externalKey]) {
storeValue[localKey] = externalState[externalKey];
return storeValue;
}
return storeValue;
return { ...storeValue, [localKey]: store[externalKey] };
});
}
});
onDestroy(unsubscribe);
} else {
const externalStore = getCompositeStore(externalID);
if (externalStore) {
const unsubscribe = externalStore.subscribe((externalState) => {
if (externalState && externalKey in externalState) {
localStore.update((storeValue) => {
return { ...storeValue, [localKey]: externalState[externalKey] };
});
}
});
onDestroy(unsubscribe);
}
}
}
}