|
5 | 5 | { |
6 | 6 | "imports": { |
7 | 7 | "uhtml": "../src/dom/node.js", |
8 | | - "dom-cue": "https://esm.run/dom-cue" |
| 8 | + "dom-cue": "https://esm.run/dom-cue", |
| 9 | + "weaky": "https://esm.run/weaky" |
9 | 10 | } |
10 | 11 | } |
11 | 12 | </script> |
12 | 13 | <script type="module"> |
13 | | - import { render, html, unsafe } from 'uhtml'; |
14 | | - import { effect, signal } from 'dom-cue'; |
| 14 | +import { render, html, unsafe } from 'uhtml'; |
| 15 | +import { effect, signal } from 'dom-cue'; |
| 16 | +import { proxy, deref } from 'weaky'; |
15 | 17 |
|
16 | | - function Button(props, state) { |
17 | | - const counter = document.createTextNode('0'); |
| 18 | +const fr = new FinalizationRegistry(cleanup => { |
| 19 | + console.log('cleaning up'); |
| 20 | + cleanup(); |
| 21 | +}); |
18 | 22 |
|
19 | | - const cleanup = effect(() => { |
20 | | - counter.data = props.signal.value; |
21 | | - }); |
| 23 | +function Button(props, state) { |
| 24 | + const counter = proxy(html`0`); |
| 25 | + const node = html` |
| 26 | + <button class="counter" onclick=${() => props.signal.value++}> |
| 27 | + ${props.text} |
| 28 | + ${deref(counter)} |
| 29 | + </button> |
| 30 | + `; |
22 | 31 |
|
23 | | - return html` |
24 | | - <button onclick=${() => props.signal.value++}> |
25 | | - ${props.text} |
26 | | - ${counter} |
27 | | - </button> |
28 | | - `; |
29 | | - } |
| 32 | + fr.register(node, effect(() => { |
| 33 | + counter.data = props.signal.value; |
| 34 | + })); |
| 35 | + |
| 36 | + return node; |
| 37 | +} |
30 | 38 |
|
31 | | - const ref = { current: null }; |
32 | | - render(document.body, () => html` |
33 | | - <h1 key=${1} ref=${ref} class=${'title'} onclick=${() => alert('Hello DOM!')}> |
34 | | - Hello ${[html`DOM`]} ! |
35 | | - </h2> |
36 | | - ${unsafe('<hr />')} |
37 | | - <${Button} text=${'π'} signal=${signal(0)} /> |
38 | | - <${Button} text=${'π'} signal=${signal(0)} /> |
39 | | - <${Button} text=${'π'} signal=${signal(0)} /> |
40 | | - `); |
| 39 | +const ref = { current: null }; |
| 40 | +render(document.body, () => html` |
| 41 | + <h1 key=${1} ref=${ref} class=${'title'} onclick=${() => alert('Hello DOM!')}> |
| 42 | + Hello ${[html`DOM`]} ! |
| 43 | + </h2> |
| 44 | + ${unsafe('<hr />')} |
| 45 | + <${Button} text=${'π'} signal=${signal(0)} /> |
| 46 | + <${Button} text=${'π'} signal=${signal(0)} /> |
| 47 | + <${Button} text=${'π'} signal=${signal(0)} /> |
| 48 | +`); |
41 | 49 |
|
42 | | - console.log(ref.current); |
| 50 | +console.log(ref.current); |
| 51 | +ref.current = null; |
43 | 52 | </script> |
0 commit comments