-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (51 loc) · 1.55 KB
/
Copy pathmain.go
File metadata and controls
65 lines (51 loc) · 1.55 KB
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
package main
import (
"fmt"
"github.com/base-go/GoFlow/pkg/core/signals"
)
func main() {
fmt.Println("=== Counter Application ===")
// State
count := signals.New(0)
multiplier := signals.New(2)
// Derived state
doubled := signals.NewComputed(func() int {
return count.Get() * 2
})
tripled := signals.NewComputed(func() int {
return count.Get() * 3
})
customMultiplied := signals.NewComputed(func() int {
return count.Get() * multiplier.Get()
})
// Effects
dispose1 := signals.NewEffect(func() {
fmt.Printf("Count changed: %d\n", count.Get())
})
defer dispose1()
dispose2 := signals.NewEffect(func() {
// Use Peek to avoid reacting to every count change
currentCount := signals.Untracked(func() int {
return count.Peek()
})
mult := multiplier.Get()
fmt.Printf("Multiplier changed to %d (count is %d)\n", mult, currentCount)
})
defer dispose2()
// Simulate app interactions
fmt.Println("\n--- Incrementing counter ---")
count.Update(func(v int) int { return v + 1 })
count.Update(func(v int) int { return v + 1 })
fmt.Println("\n--- Checking computed values ---")
fmt.Printf("Doubled: %d\n", doubled.Get())
fmt.Printf("Tripled: %d\n", tripled.Get())
fmt.Printf("Custom (×%d): %d\n", multiplier.Peek(), customMultiplied.Get())
fmt.Println("\n--- Changing multiplier ---")
multiplier.Set(5)
fmt.Println("\n--- Batch increment and multiply ---")
signals.Batch(func() {
count.Update(func(v int) int { return v + 3 })
multiplier.Set(10)
})
fmt.Printf("Final custom (×%d): %d\n", multiplier.Peek(), customMultiplied.Get())
}