cleanup of composer

This commit is contained in:
Samuel Andert
2023-08-04 18:53:14 +02:00
parent aced9fca2b
commit 6f9c2da0ac
5 changed files with 25 additions and 76 deletions

View File

@ -6,6 +6,7 @@
import { createComposerStore, getComposerStore } from './composerStores';
import { coreServices } from './coreServices';
import { Machine, interpret } from 'xstate';
import { subscribeAndMapQueries } from './queryLoader';
interface IComposerLayout {
areas: string;
@ -29,7 +30,6 @@
export let composer: IComposer;
let layoutStyle = '';
let machineService;
let unsubscribers = [];
$: {
@ -42,6 +42,17 @@
initializeComposerState(child);
});
}
initializeAndStartMachine(composer);
if (composer?.children) {
composer.children.forEach((child) => {
initializeAndStartMachine(child);
});
}
}
function initializeAndStartMachine(composer: IComposer) {
if (composer?.machine) {
const machine = Machine(
{
@ -52,7 +63,7 @@
services: composer.machine.services
}
);
machineService = interpret(machine).onTransition((state) => {
const machineService = interpret(machine).onTransition((state) => {
console.log(`${composer.id} State transitioned to: ${state.value}`);
console.log('Context:', state.context); // Log the context
getComposerStore(composer.id).update((storeValue) => ({
@ -64,40 +75,6 @@
machineService.start();
composer.machineService = machineService;
}
if (composer?.children) {
composer.children.forEach((child) => {
if (child.machine) {
const childMachine = Machine(
{ ...child.machine, id: child.id },
{
services: child.machine.services
}
);
machineService = interpret(childMachine).onTransition((state) => {
console.log(`${child.id} State transitioned to: ${state.value}`);
console.log('Context:', state.context); // Log the context
getComposerStore(child.id).update((storeValue) => ({
...storeValue,
state: state.value, // updated
context: state.context // added
}));
});
machineService.start();
child.machineService = machineService;
}
});
}
}
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 {