-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.ts
61 lines (57 loc) · 1.32 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// A version of "basics.ts" using state and provider.
import {
DenierState,
build,
denierApp,
html,
on,
provide,
using,
} from "../src";
// A state holding our counter value.
class CounterState extends DenierState<{ count: number }> {
constructor() {
super({ count: 42 });
}
}
const app = html`
<div
${
// Use the `provide` attribute directive to make the state
// available for child nodes to consume.
provide(() => {
const state = new CounterState();
setInterval(() => state.set({ count: state.get().count + 1 }), 500);
return state;
})
}
>
${
// Build a template, initiated with the provided state.
// The template gets updated on state changes.
build(
CounterState,
(s) =>
html`
<div>
Initial: ${s.get().count}, Current: ${() => s.get().count}
</div>
`
)
}
${
// Build a template using a provided object. In this case,
// it's our state.
using(
CounterState,
(s) => html` <button ${on("click", () => s.update({ count: 0 }))}>
Reset
</button>`
)
}
</div>
`;
addEventListener("DOMContentLoaded", () => {
const root = document.getElementById("state-app");
denierApp(app, root!);
});