fixed a bug of dynamic machine service initializded in in composite

This commit is contained in:
Samuel Andert 2023-08-03 13:09:37 +02:00
parent 3aa211a35d
commit 37b915cef6
4 changed files with 22 additions and 31 deletions

View File

@ -2,15 +2,18 @@
export let services; export let services;
export let store; export let store;
export let machineService;
let childStore; let childStore;
$: if (services.core) { $: if (services.core) {
childStore = services.core.subscribeComposite('@child'); childStore = services.core.subscribeComposite('@learnready');
} }
$: { $: {
if ($childStore && $childStore.machine.state) { if ($childStore && $childStore.machine.state) {
services.machine.send($childStore.machine.state); console.log('learn color machine: ' + JSON.stringify(machineService));
machineService.send($childStore.machine.state);
} }
} }
</script> </script>
@ -22,4 +25,3 @@
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: {$store.machine.state}; border-radius: 50%; width: 50px; height: 50px;"
/> />
<p>The child state: {JSON.stringify($childStore)}</p>

View File

@ -1,29 +1,19 @@
<script> <script>
import { interpret, Machine } from 'xstate';
export let store; export let store;
let service; export let services;
let current; export let machineService;
export let machine;
let compositeMachine; const handleButton = () => {
console.log('learn ready machine: ' + JSON.stringify(machineService));
$: if (machine) { console.log('Sending TOGGLE event to the machine');
compositeMachine = Machine(machine); machineService.send('TOGGLE');
};
current = compositeMachine.initialState.value;
service = interpret(compositeMachine).onTransition((state) => {
$store.machine.state = state.value;
});
service.start();
}
</script> </script>
<div class="border-2 border-yellow-500"> <div class="border-2 border-yellow-500">
i am the child and this is my state: {$store.machine.state} i am the child and this is my state: {$store.machine.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={() => service.send('TOGGLE')}>Switch</button on:click={handleButton}>Switch</button
> >
</div> </div>

View File

@ -1,10 +1,7 @@
<!-- src/lib/components/Recipies/oRecipe.svelte --> <!-- src/lib/components/Recipies/oRecipe.svelte -->
<script> <script>
import { interpret } from 'xstate';
import Composite from '$lib/core/Composite.svelte'; import Composite from '$lib/core/Composite.svelte';
// export let machine;
let composite = { let composite = {
id: 'testingstuff', id: 'testingstuff',
layout: { layout: {
@ -15,14 +12,13 @@
}, },
children: [ children: [
{ {
id: 'child', id: 'learnready',
component: 'LearnReady', component: 'LearnReady',
slot: 'left', slot: 'left',
store: { store: {
machine: { state: 'NOTREADY' } machine: { state: 'NOTREADY' }
}, },
machine: { machine: {
id: 'compositemachine',
initial: 'NOTREADY', initial: 'NOTREADY',
states: { states: {
NOTREADY: { NOTREADY: {
@ -39,7 +35,6 @@
component: 'LearnColor', component: 'LearnColor',
slot: 'right', slot: 'right',
machine: { machine: {
id: 'color',
initial: 'RED', initial: 'RED',
states: { states: {
GREEN: { GREEN: {

View File

@ -28,6 +28,7 @@
children?: IComposite[]; children?: IComposite[];
servicesLoaded?: boolean; servicesLoaded?: boolean;
machine?: any; machine?: any;
machineService?: any;
} }
export let composite: IComposite; export let composite: IComposite;
@ -35,6 +36,7 @@
core: coreServices core: coreServices
}; };
let layoutStyle = ''; let layoutStyle = '';
let machineService; let machineService;
$: { $: {
@ -52,7 +54,7 @@
}); });
} }
if (composite?.machine) { if (composite?.machine) {
const machine = Machine(composite.machine); const machine = Machine({ ...composite.machine, id: composite.id });
machineService = interpret(machine).onTransition((state) => { machineService = interpret(machine).onTransition((state) => {
getCompositeStore(composite.id).update((storeValue) => ({ getCompositeStore(composite.id).update((storeValue) => ({
...storeValue, ...storeValue,
@ -60,13 +62,13 @@
})); }));
}); });
machineService.start(); machineService.start();
loadedServices.machine = machineService; composite.machineService = machineService;
} }
if (composite?.children) { if (composite?.children) {
composite.children.forEach((child) => { composite.children.forEach((child) => {
if (child.machine) { if (child.machine) {
const childMachine = Machine(child.machine); const childMachine = Machine({ ...child.machine, id: child.id });
machineService = interpret(childMachine).onTransition((state) => { machineService = interpret(childMachine).onTransition((state) => {
getCompositeStore(child.id).update((storeValue) => ({ getCompositeStore(child.id).update((storeValue) => ({
...storeValue, ...storeValue,
@ -74,7 +76,7 @@
})); }));
}); });
machineService.start(); machineService.start();
loadedServices.machine = machineService; child.machineService = machineService;
} }
}); });
} }
@ -217,6 +219,7 @@
store={getCompositeStore(composite.id)} store={getCompositeStore(composite.id)}
machine={composite.machine} machine={composite.machine}
services={loadedServices} services={loadedServices}
machineService={child.machineService}
/> />
{/await} {/await}
{/if} {/if}
@ -234,6 +237,7 @@
store={getCompositeStore(child.id)} store={getCompositeStore(child.id)}
machine={child.machine} machine={child.machine}
services={loadedServices} services={loadedServices}
machineService={child.machineService}
/> />
{#if child.children && child.children.length} {#if child.children && child.children.length}
<Composite composite={child} /> <Composite composite={child} />