-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
69 lines (54 loc) · 1.89 KB
/
app.js
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
62
63
64
65
66
67
68
69
import { computed } from "./lib/computed.js";
import { effect } from "./lib/effect.js";
import { signal } from "./lib/signal.js";
const count = signal(0);
const name = signal("John");
const doubled = computed(() => {
console.log("Computing `doubled`...");
return count.get() * 2;
});
const greeting = computed(() => {
console.log("Computing `greeting`...");
return `Hello ${name.get()}, count is ${count.get()}`;
});
// Effects for UI updates
const countDisplay = document.createElement("div");
const nameInput = document.createElement("input");
const buttons = document.createElement("div");
document.body.append(countDisplay, nameInput, buttons);
// Effect for updating count display
effect(() => {
console.log("Running `count` effect...");
countDisplay.textContent = `Count: ${count.get()} (doubled: ${doubled()})`;
});
// Effect for name syncing
effect(() => {
console.log("Running `name` effect...");
nameInput.value = name.get();
});
// Add input handler
nameInput.addEventListener("input", (event) => {
name.set(event.target.value);
});
// Add increment/decrement buttons
const incrementBtn = document.createElement("button");
incrementBtn.textContent = "+";
incrementBtn.onclick = () => count.set(count.get() + 1);
const decrementBtn = document.createElement("button");
decrementBtn.textContent = "-";
decrementBtn.onclick = () => count.set(count.get() - 1);
// Effect cleanup demo button
const cleanupBtn = document.createElement("button");
cleanupBtn.textContent = "Stop count effect";
const stopCount = effect(() => {
const cleanup = () => console.log("Cleaning up count watcher...");
console.log(`Count changed to: ${count.get()}`);
return cleanup;
});
cleanupBtn.onclick = () => {
stopCount();
console.log("Stopped count watcher");
};
buttons.append(decrementBtn, incrementBtn, cleanupBtn);
// Log divider for console readability
console.log("App initialized" + "-".repeat(20));