refactoring next part
This commit is contained in:
parent
481cc8cf68
commit
8214792f01
@ -1,29 +1,64 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import { coreServices } from '$lib/core/refactor/coreServices';
|
||||||
|
import { queryMessages } from '$lib/data/queryMessages';
|
||||||
|
import { queryTodos } from '$lib/data/queryTodos';
|
||||||
|
import { createMessage } from '$lib/services/messages/messages';
|
||||||
export let me;
|
export let me;
|
||||||
export let data;
|
export let data;
|
||||||
|
|
||||||
function toggle() {
|
function sendMessage() {
|
||||||
me.do.machine.send('TOGGLE');
|
const randomNumber = Math.floor(Math.random() * 100) + 1;
|
||||||
|
const messageData = {
|
||||||
|
text: `test ${randomNumber}`,
|
||||||
|
sender: 'ComposerCharly',
|
||||||
|
type: 'text'
|
||||||
|
};
|
||||||
|
createMessage(messageData);
|
||||||
|
}
|
||||||
|
|
||||||
|
let todos;
|
||||||
|
queryTodos.subscribe((value) => {
|
||||||
|
todos = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
let messages;
|
||||||
|
queryMessages.subscribe((value) => {
|
||||||
|
messages = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
$: {
|
||||||
|
const queryMap = {
|
||||||
|
todos: todos,
|
||||||
|
messages: messages
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.entries(queryMap).forEach(([name, data]) => {
|
||||||
|
if (data) {
|
||||||
|
coreServices.mutateStore(me.id, { [name]: data });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="w-full h-full border-2 border-green-500">
|
<div class="flex flex-col h-screen">
|
||||||
<p>My ID is: {me.id}</p>
|
<section class=" h-96">
|
||||||
my state is {$data.state}
|
{#if $data.store.todos}
|
||||||
<h1 class="h1">I am charly</h1>
|
{#each $data.store.todos as todo}
|
||||||
my data.context.hello prop: {JSON.stringify($data.context.hello)}
|
<p>{todo.text}</p>
|
||||||
<br />
|
{/each}
|
||||||
<button
|
{/if}
|
||||||
class="px-4 py-2 font-bold text-white bg-blue-500 rounded hover:bg-blue-700"
|
</section>
|
||||||
on:click={toggle}
|
<section class="flex-grow overflow-y-auto">
|
||||||
>
|
{#if $data.store.messages}
|
||||||
Toggle
|
{#each $data.store.messages as message}
|
||||||
</button>
|
|
||||||
<div class="p-8 overflow-scroll h-1/2">
|
|
||||||
{#if $data.context.messages}
|
|
||||||
{#each $data.context.messages as message}
|
|
||||||
<p>{message.text}</p>
|
<p>{message.text}</p>
|
||||||
{/each}
|
{/each}
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</section>
|
||||||
|
<button
|
||||||
|
class="self-center px-4 py-2 mb-4 font-bold text-white rounded hover:bg-blue-700"
|
||||||
|
on:click={sendMessage}
|
||||||
|
>
|
||||||
|
Send
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
import { assign } from 'xstate';
|
import { assign } from 'xstate';
|
||||||
import { queryMessages } from '$lib/data/queryMessages';
|
import { queryMessages } from '$lib/data/queryMessages';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
|
import { coreServices } from '$lib/core/refactor/coreServices';
|
||||||
|
|
||||||
let composer = {
|
let composer = {
|
||||||
id: 'ComposerParent',
|
id: 'ComposerParent',
|
||||||
@ -30,18 +31,15 @@
|
|||||||
id: 'ComposerCharly',
|
id: 'ComposerCharly',
|
||||||
component: 'ComposerCharly',
|
component: 'ComposerCharly',
|
||||||
slot: 'aside',
|
slot: 'aside',
|
||||||
data: {},
|
|
||||||
machine: {
|
machine: {
|
||||||
initial: 'LOADING',
|
initial: 'LOADING',
|
||||||
context: {
|
// context: {
|
||||||
hello: 'start',
|
// hello: 'start',
|
||||||
messages: []
|
// messages: []
|
||||||
},
|
// },
|
||||||
states: {
|
states: {
|
||||||
LOADING: {
|
LOADING: {
|
||||||
entry: assign({
|
entry: () => {},
|
||||||
messages: (context, event) => get(queryMessages)
|
|
||||||
}),
|
|
||||||
on: {
|
on: {
|
||||||
TOGGLE: {
|
TOGGLE: {
|
||||||
target: 'READY'
|
target: 'READY'
|
||||||
|
0
src/lib/core/refactor/ComposerComponent.svelt
Normal file
0
src/lib/core/refactor/ComposerComponent.svelt
Normal file
38
src/lib/core/refactor/ComposerComponent.svelte
Normal file
38
src/lib/core/refactor/ComposerComponent.svelte
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { onDestroy } from 'svelte';
|
||||||
|
import { getComposerStore } from './composerStores';
|
||||||
|
import { Machine, interpret } from 'xstate';
|
||||||
|
|
||||||
|
export let composer;
|
||||||
|
export let Component;
|
||||||
|
export let loadedServices;
|
||||||
|
let machineService;
|
||||||
|
|
||||||
|
$: {
|
||||||
|
if (composer?.machine) {
|
||||||
|
const machine = Machine({ ...composer.machine, id: composer.id });
|
||||||
|
machineService = interpret(machine).onTransition((state) => {
|
||||||
|
getComposerStore(composer.id).update((storeValue) => ({
|
||||||
|
...storeValue,
|
||||||
|
machine: { state: state.value }
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
machineService.start();
|
||||||
|
composer.machineService = machineService;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
|
machineService?.stop();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<svelte:component
|
||||||
|
this={Component}
|
||||||
|
id={composer.id}
|
||||||
|
store={getComposerStore(composer.id)}
|
||||||
|
machine={composer.machine}
|
||||||
|
services={loadedServices}
|
||||||
|
machineService={composer.machineService}
|
||||||
|
me={composer.me}
|
||||||
|
/>
|
@ -2,14 +2,14 @@
|
|||||||
import { getComposerStore } from './composerStores';
|
import { getComposerStore } from './composerStores';
|
||||||
|
|
||||||
export const coreServices = {
|
export const coreServices = {
|
||||||
// mutateData: (storeID: string, value: any) => {
|
mutateStore: (storeID: string, value: any) => {
|
||||||
// const store = getComposerStore(storeID);
|
const store = getComposerStore(storeID);
|
||||||
// console.log(`mutateData called with storeID: ${storeID} and value: ${JSON.stringify(value)}`);
|
console.log(`mutateData called with storeID: ${storeID} and value: ${JSON.stringify(value)}`);
|
||||||
// store.update(storeData => {
|
store.update(storeData => {
|
||||||
// storeData.context = value;
|
storeData.store = { ...storeData.store, ...value };
|
||||||
// return storeData;
|
return storeData;
|
||||||
// });
|
});
|
||||||
// },
|
},
|
||||||
subscribeData: (storeID: string) => {
|
subscribeData: (storeID: string) => {
|
||||||
const store = getComposerStore(storeID);
|
const store = getComposerStore(storeID);
|
||||||
return store;
|
return store;
|
||||||
|
16
src/lib/core/refactor/dataLoader.ts
Normal file
16
src/lib/core/refactor/dataLoader.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// dataLoader.ts
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
import dataSources from 'virtual:data-sources-list';
|
||||||
|
|
||||||
|
// The store that holds the data sets
|
||||||
|
export const dataStore = writable({});
|
||||||
|
|
||||||
|
// Dynamically import the data modules and assign them to the store
|
||||||
|
dataSources.forEach(src => {
|
||||||
|
import(`/src/lib/data/${src}.ts`).then(module => {
|
||||||
|
// Here, explicitly extract the required data or function from the module
|
||||||
|
const moduleData = module[src] || module.default;
|
||||||
|
|
||||||
|
dataStore.update(store => ({ ...store, [src]: moduleData }));
|
||||||
|
});
|
||||||
|
});
|
@ -4,14 +4,17 @@ import { writable } from 'svelte/store';
|
|||||||
export const queryTodos = writable([
|
export const queryTodos = writable([
|
||||||
{
|
{
|
||||||
id: "id1",
|
id: "id1",
|
||||||
text: "todo 1"
|
text: "todo 1",
|
||||||
|
type: "x"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "id2",
|
id: "id2",
|
||||||
text: "todo 2"
|
text: "todo 2",
|
||||||
|
type: "x"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "id3",
|
id: "id3",
|
||||||
text: "todo 3"
|
text: "todo 3",
|
||||||
|
type: "z"
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user