Skip to content

Commit 17bfaf9

Browse files
committed
revert
1 parent c5633b1 commit 17bfaf9

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

demo/src/routes/stores.svelte

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<script lang="ts">
2+
import Button from "$lib/components/ui/button/button.svelte";
3+
import { unregister } from "$lib/registry.svelte";
4+
import { ComponentCompiler, type CompiledComponent } from "@mateothegreat/dynamic-component-engine";
5+
import { onDestroy, onMount, type Component } from "svelte";
6+
import { writable } from "svelte/store";
7+
8+
let renderRef: HTMLDivElement | undefined = $state(undefined);
9+
let instance: CompiledComponent | undefined = $state(undefined);
10+
let RenderableComponent = $state<Component | null>(null);
11+
let instances: CompiledComponent[] = $state([]);
12+
13+
const s = writable({ count: 0 });
14+
let count = $derived(s);
15+
16+
// s.subscribe((value) => {
17+
// count = value.count;
18+
// console.log("Initial props:", s, value, count);
19+
// });
20+
21+
const doWork = async () => {
22+
const source = `
23+
<script>
24+
import { onDestroy } from "svelte";
25+
import Button from "$lib/components/ui/button/button.svelte";
26+
27+
let { id, s } = $props();
28+
29+
let count = $derived(s.count);
30+
31+
s.subscribe((value) => {
32+
count = value.count;
33+
console.log("Initial props:", s, value, count);
34+
});
35+
36+
$effect(() => {
37+
console.log("count", count);
38+
});
39+
40+
const update = (props) => {
41+
console.log("dynamic component update() triggered", props);
42+
};
43+
44+
const incrementCount = () => {
45+
s.update((prev) => ({ ...prev, count: prev.count + 1 }));
46+
console.log("Incremented from inside, new value:", s.count);
47+
};
48+
<\/script>
49+
50+
<div class="flex flex-col gap-2 m-4 p-4 border-2 border-sky-700 rounded bg-gray-800/35 text-sm">
51+
<div class="">
52+
<span class="text-sm text-fuchsia-500 font-bold">{id}</span>
53+
<span class="text-sm text-slate-500">(dynamically rendered component)</span>
54+
</div>
55+
<div class="">
56+
<p>Count: <span class="text-green-400">{count}</span></p>
57+
<button onclick={incrementCount} class="mt-4 px-2 text-sm py-1 bg-blue-500 text-slate-200 rounded hover:bg-blue-600">
58+
s.count++ (from inside)
59+
</button>
60+
</div>
61+
</div>`;
62+
63+
const id = Math.random().toString(36).substring(2, 15);
64+
const compiled = ComponentCompiler.compile(source);
65+
console.log(compiled);
66+
const loaded = await ComponentCompiler.load(compiled.js.code);
67+
console.log(loaded);
68+
console.log(
69+
await ComponentCompiler.render(id, source, renderRef!, {
70+
id,
71+
s
72+
})
73+
);
74+
75+
// const transformed = ComponentCompiler.wrap(compiled.js.code);
76+
// const fn = await ComponentCompiler.load(transformed.data);
77+
// const component = fn(renderRef!, {
78+
// id,
79+
// p,
80+
// count: p.count
81+
// });
82+
83+
// RenderableComponent = fn().component;
84+
85+
// console.log(RenderableComponent);
86+
87+
let interval = setInterval(() => {
88+
s.update((prev) => ({ ...prev, count: prev.count + 1 }));
89+
}, 750);
90+
};
91+
92+
const destroyComponent = () => {
93+
instances.forEach((instance) => {
94+
console.log("runes-registry: destroying component:", instance.id);
95+
instance.destroy();
96+
unregister(instance.id);
97+
});
98+
instances = [];
99+
if (renderRef) {
100+
renderRef.innerHTML = "";
101+
}
102+
};
103+
104+
onMount(async () => {
105+
await doWork();
106+
});
107+
108+
onDestroy(() => {
109+
destroyComponent();
110+
});
111+
</script>
112+
113+
<div class="flex flex-col gap-2 m-4">
114+
<h1 class="text-2xl font-bold">Shared Store</h1>
115+
<p class="text-sm text-slate-400">The store is updated every 750ms and the component automatically reflects the new value.</p>
116+
<div class="flex flex-col gap-4">
117+
<div class="flex gap-4">
118+
<Button onclick={doWork}>Create another Component</Button>
119+
<Button onclick={destroyComponent} variant="destructive">Destroy all Components</Button>
120+
</div>
121+
<div class="flex bg-gray-800/20 p-1.5 px-2 rounded border-2 border-gray-800/50">
122+
<div class="flex items-center justify-center gap-2">
123+
<div class="text-sm text-slate-500">
124+
(outside)
125+
<span class="font-bold text-indigo-400">count</span>
126+
:
127+
</div>
128+
<div class="text-green-500 bg-gray-800 rounded px-2 text-sm font-bold">{count}</div>
129+
<Button size="sm" variant="outline" onclick={() => s.update((prev) => ({ ...prev, count: prev.count + 1 }))}>Increment</Button>
130+
</div>
131+
</div>
132+
<div bind:this={renderRef} class="p-4 rounded border-2 border-gray-800 bg-gray-800/20"></div>
133+
</div>
134+
</div>

0 commit comments

Comments
 (0)