-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (84 loc) · 2.18 KB
/
Copy pathmain.go
File metadata and controls
100 lines (84 loc) · 2.18 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"github.com/base-go/GoFlow/pkg/core/signals"
)
type Todo struct {
ID int
Text string
Completed bool
}
func main() {
fmt.Println("=== Todo List Example with SignalSlice ===")
// Reactive todo list
todos := signals.NewSlice([]Todo{})
nextID := 1
// Computed values
totalCount := todos.Len()
completedCount := signals.NewComputed(func() int {
count := 0
for _, todo := range todos.Get() {
if todo.Completed {
count++
}
}
return count
})
remainingCount := signals.NewComputed(func() int {
return totalCount.Get() - completedCount.Get()
})
// Effect to display status
dispose := signals.NewEffect(func() {
total := totalCount.Get()
completed := completedCount.Get()
remaining := remainingCount.Get()
fmt.Printf("\n📊 Status: %d total, %d completed, %d remaining\n", total, completed, remaining)
})
defer dispose()
// Add todos
fmt.Println("\n➕ Adding todos...")
todos.Append(Todo{ID: nextID, Text: "Learn Go", Completed: false})
nextID++
todos.Append(Todo{ID: nextID, Text: "Build GoFlow", Completed: false})
nextID++
todos.Append(Todo{ID: nextID, Text: "Write examples", Completed: false})
nextID++
// Complete a todo
fmt.Println("\n✅ Completing 'Learn Go'...")
todos.Update(func(list []Todo) []Todo {
newList := make([]Todo, len(list))
copy(newList, list)
for i, todo := range newList {
if todo.Text == "Learn Go" {
newList[i].Completed = true
break
}
}
return newList
})
// Add multiple todos with batch
fmt.Println("\n➕ Adding multiple todos (batched)...")
signals.Batch(func() {
todos.Append(Todo{ID: nextID, Text: "Test signals", Completed: false})
nextID++
todos.Append(Todo{ID: nextID, Text: "Optimize performance", Completed: true})
nextID++
})
// Display all todos
fmt.Println("\n📝 All todos:")
for _, todo := range todos.Get() {
status := "⬜"
if todo.Completed {
status = "✅"
}
fmt.Printf(" %s %s\n", status, todo.Text)
}
// Filter active todos
activeTodos := todos.Filter(func(todo Todo) bool {
return !todo.Completed
})
fmt.Println("\n🎯 Active todos:")
for _, todo := range activeTodos.Get() {
fmt.Printf(" - %s\n", todo.Text)
}
}