-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (64 loc) · 1.97 KB
/
Copy pathmain.go
File metadata and controls
79 lines (64 loc) · 1.97 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
package main
import (
"fmt"
"log"
"time"
eventgoround "github.com/tanerius/EventGoRound/v2"
)
// SimpleRegistry implements the IEventRegistry interface
type SimpleRegistry struct {
handlers map[string]func(any)
}
func NewSimpleRegistry() *SimpleRegistry {
return &SimpleRegistry{
handlers: make(map[string]func(any)),
}
}
func (r *SimpleRegistry) RegisterHandler(name string, handler func(any)) {
r.handlers[name] = handler
}
func (r *SimpleRegistry) GetHandler(name string) (func(any), error) {
handler, exists := r.handlers[name]
if !exists {
return nil, fmt.Errorf("handler not found: %s", name)
}
return handler, nil
}
func main() {
// Create a registry and register event handlers
registry := NewSimpleRegistry()
registry.RegisterHandler("greet", func(payload any) {
name := payload.(string)
fmt.Printf("Hello, %s!\n", name)
})
registry.RegisterHandler("calculate", func(payload any) {
numbers := payload.([]int)
sum := 0
for _, n := range numbers {
sum += n
}
fmt.Printf("Sum: %d\n", sum)
})
// Create event loop with 100ms tick interval and enable logging
// Logs will be written to events.log with automatic rotation at 10MB
// IncludeInfo: true means both ERROR and INFO logs are written
logConfig := &eventgoround.LogConfig{
Enabled: true,
FilePath: "./events.log",
IncludeInfo: true, // Set to false to log only ERRORs
}
eventLoop := eventgoround.NewEventLoop(100*time.Millisecond, registry, logConfig)
// To disable logging entirely, pass nil: eventgoround.NewEventLoop(100*time.Millisecond, registry, nil)
eventLoop.Start()
// Schedule an immediate event
now := time.Now().Unix()
eventLoop.ScheduleEvent(now, 0, "greet", "World")
// Schedule an event 1 second in the future
future := time.Now().Add(1 * time.Second).Unix()
eventLoop.ScheduleEvent(future, 0, "calculate", []int{1, 2, 3, 4, 5})
// Let events execute
time.Sleep(2 * time.Second)
// Stop the event loop
eventLoop.Stop()
log.Println("Event loop stopped")
}