Refactoring next part

This commit is contained in:
Samuel Andert 2023-08-04 11:12:24 +02:00
parent c0d29e784a
commit 4a01649087
6 changed files with 26 additions and 29 deletions

View File

@ -1,7 +1,6 @@
<script> <script>
import { onMount, afterUpdate } from 'svelte'; import { onMount, afterUpdate } from 'svelte';
import Composite from '$lib/core/Composite.svelte'; import Composite from '$lib/core/Composite.svelte';
import { Avatar } from '@skeletonlabs/skeleton';
export let store; export let store;

View File

@ -1,19 +1,16 @@
<script> <script>
export let me; export let me;
export let services; export let data;
export let store;
export let machineService; let childData;
let childStore;
$: if (me.do) { $: if (me.do) {
childStore = me.do.core.subscribeComposer('@ComposerBob'); childData = me.do.core.subscribeData('@ComposerBob');
} }
$: { $: {
if ($childStore && $childStore.machine.state) { if ($childData && $childData.state) {
me.do.machine.send($childStore.machine.state); me.do.machine.send($childData.state);
} }
} }
</script> </script>
@ -21,9 +18,9 @@
<div class="p-2 border-2 border-blue-500"> <div class="p-2 border-2 border-blue-500">
<p>My ID is: {me.id}</p> <p>My ID is: {me.id}</p>
I am the parent, this is my state: {$store.machine.state} I am the parent, this is my state: {$data.state}
<div <div
class="p-2 border-2" class="p-2 border-2"
style="background-color: {$store.machine.state}; border-radius: 50%; width: 50px; height: 50px;" style="background-color: {$data.state}; border-radius: 50%; width: 50px; height: 50px;"
/> />
</div> </div>

View File

@ -1,12 +1,9 @@
<script> <script>
export let me; export let me;
export let store; export let data;
export let machineService;
const handleButton = () => { const handleButton = () => {
if (me && me.do && me.do.machine) { if (me && me.do && me.do.machine) {
console.log('BOB machine service: ' + JSON.stringify(me.do.machine));
console.log('Sending TOGGLE event to the machine');
me.do.machine.send('TOGGLE'); me.do.machine.send('TOGGLE');
} }
}; };
@ -15,7 +12,7 @@
<div class="border-2 border-yellow-500"> <div class="border-2 border-yellow-500">
<p>My ID is: {me.id}</p> <p>My ID is: {me.id}</p>
i am the child and this is my state: {$store.machine.state} i am the child and this is my state: {$data.state}
<button <button
class="px-4 py-2 font-bold text-white bg-blue-500 rounded hover:bg-blue-700" class="px-4 py-2 font-bold text-white bg-blue-500 rounded hover:bg-blue-700"
on:click={handleButton}>Switch</button on:click={handleButton}>Switch</button

View File

@ -26,6 +26,9 @@
{ {
id: 'ComposerCharly', id: 'ComposerCharly',
component: 'ComposerCharly', component: 'ComposerCharly',
data: {
context: { messenges: 'put the messengesStore here' }
},
slot: 'aside', slot: 'aside',
machine: { machine: {
initial: 'NOTREADY', initial: 'NOTREADY',
@ -43,7 +46,7 @@
id: 'ComposerBob', id: 'ComposerBob',
component: 'ComposerBob', component: 'ComposerBob',
slot: 'top', slot: 'top',
store: { data: {
machine: { state: 'NOTREADY' } machine: { state: 'NOTREADY' }
}, },
machine: { machine: {

View File

@ -18,7 +18,7 @@
interface IComposer { interface IComposer {
layout?: IComposerLayout; layout?: IComposerLayout;
id: string; id: string;
me?: { id: string; do: any }; me?: { id: string; do: any; data: any };
slot?: string; slot?: string;
component?: string; component?: string;
store?: Record<string, any>; store?: Record<string, any>;
@ -27,9 +27,10 @@
} }
export let composer: IComposer; export let composer: IComposer;
let layoutStyle = '';
let layoutStyle = '';
let machineService; let machineService;
let unsubscribers = [];
$: { $: {
layoutStyle = computeLayoutStyle(composer?.layout); layoutStyle = computeLayoutStyle(composer?.layout);
@ -46,7 +47,8 @@
machineService = interpret(machine).onTransition((state) => { machineService = interpret(machine).onTransition((state) => {
getComposerStore(composer.id).update((storeValue) => ({ getComposerStore(composer.id).update((storeValue) => ({
...storeValue, ...storeValue,
machine: { state: state.value } state: state.value, // updated
context: state.context // added
})); }));
}); });
machineService.start(); machineService.start();
@ -60,7 +62,8 @@
machineService = interpret(childMachine).onTransition((state) => { machineService = interpret(childMachine).onTransition((state) => {
getComposerStore(child.id).update((storeValue) => ({ getComposerStore(child.id).update((storeValue) => ({
...storeValue, ...storeValue,
machine: { state: state.value } state: state.value, // updated
context: state.context // added
})); }));
}); });
machineService.start(); machineService.start();
@ -92,8 +95,6 @@
} }
} }
let unsubscribers = [];
onDestroy(() => { onDestroy(() => {
unsubscribers.forEach((unsub) => unsub()); unsubscribers.forEach((unsub) => unsub());
}); });
@ -120,14 +121,14 @@
</script> </script>
<div <div
class={`grid w-full h-full overflow-hidden ${composer?.layout?.tailwindClasses || ''}`} class={`grid w-full h-full overflow-hidden ${composer?.layout?.style || ''}`}
style={layoutStyle} style={layoutStyle}
> >
{#await loadComponentAndService(composer) then Component} {#await loadComponentAndService(composer) then Component}
<svelte:component <svelte:component
this={Component} this={Component}
id={composer.id} id={composer.id}
store={getComposerStore(composer.id)} data={getComposerStore(composer.id)}
machine={composer.machine} machine={composer.machine}
me={composer.me} me={composer.me}
/> />
@ -135,14 +136,14 @@
{#if composer?.children} {#if composer?.children}
{#each composer.children as child (child.id)} {#each composer.children as child (child.id)}
<div <div
class="grid w-full h-full overflow-hidden ${composer?.layout?.tailwindClasses || ''}" class="grid w-full h-full overflow-hidden ${composer?.layout?.style || ''}"
style={`grid-area: ${child.slot}`} style={`grid-area: ${child.slot}`}
> >
{#await loadComponentAndService(child) then ChildComponent} {#await loadComponentAndService(child) then ChildComponent}
<svelte:component <svelte:component
this={ChildComponent} this={ChildComponent}
id={child.id} id={child.id}
store={getComposerStore(child.id)} data={getComposerStore(child.id)}
machine={child.machine} machine={child.machine}
me={child.me} me={child.me}
/> />

View File

@ -2,7 +2,7 @@
import { getComposerStore } from './composerStores'; import { getComposerStore } from './composerStores';
export const coreServices = { export const coreServices = {
updateComposer: (mappings: Record<string, string>) => { mutateData: (mappings: Record<string, string>) => {
for (const [mappingString, value] of Object.entries(mappings)) { for (const [mappingString, value] of Object.entries(mappings)) {
const [storeID, key] = mappingString.replace('@', '').split(':'); const [storeID, key] = mappingString.replace('@', '').split(':');
const store = getComposerStore(storeID); const store = getComposerStore(storeID);
@ -12,7 +12,7 @@ export const coreServices = {
}); });
} }
}, },
subscribeComposer: (mappingString: string) => { subscribeData: (mappingString: string) => {
const [storeID] = mappingString.replace('@', '').split(':'); const [storeID] = mappingString.replace('@', '').split(':');
const store = getComposerStore(storeID); const store = getComposerStore(storeID);
return store; return store;