refactoring Composer part3

This commit is contained in:
Samuel Andert 2023-08-03 15:29:46 +02:00
parent 02d068fef3
commit 73796c756f
6 changed files with 19 additions and 84 deletions

View File

@ -1,4 +1,5 @@
<script> <script>
export let me;
export let services; export let services;
export let store; export let store;
@ -6,8 +7,8 @@
let childStore; let childStore;
$: if (services.core) { $: if (me.do) {
childStore = services.core.subscribeComposer('@ComposerBob'); childStore = me.do.subscribeComposer('@ComposerBob');
} }
$: { $: {
@ -19,6 +20,8 @@
</script> </script>
<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>
I am the parent, this is my state: {$store.machine.state} I am the parent, this is my state: {$store.machine.state}
<div <div
class="p-2 border-2" class="p-2 border-2"

View File

@ -1,4 +1,5 @@
<script> <script>
export let me;
export let store; export let store;
export let machineService; export let machineService;
@ -10,6 +11,8 @@
</script> </script>
<div class="border-2 border-yellow-500"> <div class="border-2 border-yellow-500">
<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: {$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"

View File

@ -1,52 +1,8 @@
<script> <script>
export let services; export let me;
export let store;
let isStoreLoaded = false;
$: if (services && services.helloEarthAlert) {
// services.helloEarthAlert.alertMe();
}
$: if (services.core) {
// services.core.testAlert();
}
$: if ($store) isStoreLoaded = true;
</script> </script>
{#if isStoreLoaded} <div class="border-2 border-green-500">
<div class="container p-12"> <p>My ID is: {me.id}</p>
<h1 class="h1"> <h1 class="h1">I am charly</h1>
{#if $store.title}{$store.title}{/if}
</h1>
<h2 class="py-6 h2">
{#if $store.description}{$store.description}{/if}
</h2>
<h3 class="mt-4 h3">Wallet Address</h3>
{#if $store.pkpWallet}
<p>{$store.pkpWallet.address}</p>
{/if}
<h4 class="mt-4 h4">store: hello (init default)</h4>
{#if $store.hello}{$store.hello}{/if}
<h4 class="mt-4 h4">map: hello2 : "@hello:helloMapMe"</h4>
{#if $store.hello2}{$store.hello2}{/if}
<h4 class="mt-4 h4">map: "todos": "@data:queryTodos"</h4>
{#if $store.todos}
<dl class="list-dl">
{#each $store.todos as todo}
<div>
<span class="flex-auto">
<dd>{todo.text}</dd>
</span>
</div> </div>
{/each}
</dl>
{/if}
</div>
{:else}
<div class="container">
<p>Loading...</p>
</div>
{/if}

View File

@ -3,12 +3,6 @@
let composer = { let composer = {
id: 'ComposerParent', id: 'ComposerParent',
store: {
title: 'Hello Earth',
description:
'how to use the store and mapping logic of store to store and data to store maps',
helloMapMe: 'this is going to be mapped'
},
machine: { machine: {
initial: 'NOTREADY', initial: 'NOTREADY',
states: { states: {
@ -43,20 +37,7 @@
on: { TOGGLE: 'NOTREADY' } on: { TOGGLE: 'NOTREADY' }
} }
} }
}, }
store: {
machine: { state: 'NOTREADY' },
hello: 'define me directly',
hello2: 'overwrite me with the mapping value'
},
map: {
title: '@hello:title',
description: '@hello:description',
hello2: '@hello:helloMapMe',
pkpWallet: '@wallet:pkpWallet',
todos: '@data:queryTodos'
},
services: ['helloEarthAlert']
}, },
{ {
id: 'ComposerBob', id: 'ComposerBob',

View File

@ -1,6 +1,5 @@
// coreServices.ts // coreServices.ts
import { getCompositeStore } from './compositeStores'; import { getCompositeStore } from './compositeStores';
import { interpret, Machine } from 'xstate';
export const coreServices = { export const coreServices = {
updateComposite: (mappings: Record<string, string>) => { updateComposite: (mappings: Record<string, string>) => {
@ -20,16 +19,5 @@ export const coreServices = {
}, },
testAlert: () => { testAlert: () => {
alert("core service alert") 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();
}
} }
}; };

View File

@ -2,7 +2,7 @@
import { onDestroy } from 'svelte'; import { onDestroy } from 'svelte';
import Composer from './Composer.svelte'; import Composer from './Composer.svelte';
import FallBack from './FallBack.svelte'; import FallBack from './FallBack.svelte';
import components from './componentLoader'; import components from '$lib/core/componentLoader';
import services from '$lib/core/servicesLoader'; import services from '$lib/core/servicesLoader';
import { dataStore } from '$lib/core/dataLoader'; import { dataStore } from '$lib/core/dataLoader';
import { createComposerStore, getComposerStore } from './composerStores'; import { createComposerStore, getComposerStore } from './composerStores';
@ -20,6 +20,7 @@
interface IComposer { interface IComposer {
layout?: IComposerLayout; layout?: IComposerLayout;
id: string; id: string;
me?: { id: string; do: any };
slot?: string; slot?: string;
component?: string; component?: string;
services?: string[]; services?: string[];
@ -205,6 +206,7 @@
async function loadComponentAndService(component: IComposer) { async function loadComponentAndService(component: IComposer) {
const componentName = component.component || 'FallBack'; const componentName = component.component || 'FallBack';
component.me = { id: component.id, do: loadedServices.core };
return await getComponent(componentName); return await getComponent(componentName);
} }
</script> </script>
@ -222,6 +224,7 @@
machine={composer.machine} machine={composer.machine}
services={loadedServices} services={loadedServices}
machineService={child.machineService} machineService={child.machineService}
me={composer.me}
/> />
{/await} {/await}
{/if} {/if}
@ -240,6 +243,7 @@
machine={child.machine} machine={child.machine}
services={loadedServices} services={loadedServices}
machineService={child.machineService} machineService={child.machineService}
me={child.me}
/> />
{#if child.children && child.children.length} {#if child.children && child.children.length}
<Composer composer={child} /> <Composer composer={child} />