Added dynamic instantiation of machines in composite
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
import { dataStore } from '$lib/core/dataLoader';
|
||||
import { createCompositeStore, getCompositeStore } from '$lib/core/compositeStores';
|
||||
import { coreServices } from './coreServices';
|
||||
import { Machine } from 'xstate';
|
||||
import { Machine, interpret } from 'xstate';
|
||||
|
||||
interface ICompositeLayout {
|
||||
areas: string;
|
||||
@ -35,6 +35,7 @@
|
||||
core: coreServices
|
||||
};
|
||||
let layoutStyle = '';
|
||||
let machineService;
|
||||
|
||||
$: {
|
||||
layoutStyle = computeLayoutStyle(composite?.layout);
|
||||
@ -50,6 +51,33 @@
|
||||
mapAndSubscribe(child);
|
||||
});
|
||||
}
|
||||
if (composite?.machine) {
|
||||
const machine = Machine(composite.machine);
|
||||
machineService = interpret(machine).onTransition((state) => {
|
||||
getCompositeStore(composite.id).update((storeValue) => ({
|
||||
...storeValue,
|
||||
machine: { state: state.value }
|
||||
}));
|
||||
});
|
||||
machineService.start();
|
||||
loadedServices.machine = machineService;
|
||||
}
|
||||
|
||||
if (composite?.children) {
|
||||
composite.children.forEach((child) => {
|
||||
if (child.machine) {
|
||||
const childMachine = Machine(child.machine);
|
||||
machineService = interpret(childMachine).onTransition((state) => {
|
||||
getCompositeStore(child.id).update((storeValue) => ({
|
||||
...storeValue,
|
||||
machine: { state: state.value }
|
||||
}));
|
||||
});
|
||||
machineService.start();
|
||||
loadedServices.machine = machineService;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function computeLayoutStyle(layout?: ICompositeLayout): string {
|
||||
|
@ -1,17 +1,18 @@
|
||||
// coreServices.ts
|
||||
import { getCompositeStore } from './compositeStores';
|
||||
import { interpret, Machine } from 'xstate';
|
||||
|
||||
export const coreServices = {
|
||||
// updateComposite: (mappings: Record<string, string>) => {
|
||||
// for (const [mappingString, value] of Object.entries(mappings)) {
|
||||
// const [storeID, key] = mappingString.replace('@', '').split(':');
|
||||
// const store = getCompositeStore(storeID);
|
||||
// store.update(storeData => {
|
||||
// storeData[key] = value;
|
||||
// return storeData;
|
||||
// });
|
||||
// }
|
||||
// },
|
||||
updateComposite: (mappings: Record<string, string>) => {
|
||||
for (const [mappingString, value] of Object.entries(mappings)) {
|
||||
const [storeID, key] = mappingString.replace('@', '').split(':');
|
||||
const store = getCompositeStore(storeID);
|
||||
store.update(storeData => {
|
||||
storeData[key] = value;
|
||||
return storeData;
|
||||
});
|
||||
}
|
||||
},
|
||||
subscribeComposite: (mappingString: string) => {
|
||||
const [storeID] = mappingString.replace('@', '').split(':');
|
||||
const store = getCompositeStore(storeID);
|
||||
@ -19,5 +20,16 @@ export const coreServices = {
|
||||
},
|
||||
testAlert: () => {
|
||||
alert("core service alert")
|
||||
},
|
||||
machine: {
|
||||
service: null,
|
||||
send: (event) => coreServices.machine.service.send(event),
|
||||
initialize: (machineDefinition, store) => {
|
||||
const machine = Machine(machineDefinition);
|
||||
coreServices.machine.service = interpret(machine).onTransition((state) => {
|
||||
store.update(storeValue => ({ ...storeValue, machine: { state: state.value } }));
|
||||
});
|
||||
coreServices.machine.service.start();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user