-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoeq.go
91 lines (76 loc) · 2.28 KB
/
goeq.go
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
// Package goeq provides a simple event queue that leverages Go's flavor of interfaces to connect consumers and producers of events.
package goeq
// A runner is used to run the event method in a certain context (for example in a certain goroutine).
type Runner func(func())
// The queue manages a list of consumers in order to notify them of certain events.
type Queue struct {
consumers []any
runner Runner
}
// New returns a new event queue that runs the event method in the same goroutine as the `Publish` method is called.
func New() *Queue {
return &Queue{}
}
// NewConfied returns a new event queue that uses the given runner to execute the event method.
func NewConfined(runner Runner) *Queue {
return &Queue{
runner: runner,
}
}
// Subscribe registers the given consumer with this event queue.
func (q *Queue) Subscribe(consumer any) {
q.consumers = append(q.consumers, consumer)
}
// Publish runs the given function for all subscribed consumers. Use `Publish` in conjunction with the `Event` function
// to restrict execution to the relevant consumers (those that implement the relevant interface).
func (q *Queue) Publish(notify func(item any)) {
for _, consumer := range q.consumers {
q.run(func() {
notify(consumer)
})
}
}
func (q *Queue) run(f func()) {
if q.runner == nil {
f()
return
}
q.runner(f)
}
// Event is used to restrict the execution of the event method to those consumers that implement the relevant interface.
func Event[T any](f func(T)) func(any) {
return func(item any) {
t, ok := item.(T)
if !ok {
return
}
f(t)
}
}
type goRunner struct {
events chan func()
}
// NewSyncRunner returns a runner that executes all event methods confined to the same goroutine.
// The runner returns after the event method was executed.
func NewSyncRunner() Runner {
return newRunner(0)
}
// NewAsyncRunner returns a runner that executes all event methods confined to the same goroutine.
// The runner returns immediately and does not wait until the event method was executed.
func NewAsyncRunner() Runner {
return newRunner(1)
}
func newRunner(buffer int) Runner {
result := &goRunner{
events: make(chan func(), buffer),
}
go func() {
for e := range result.events {
e()
}
}()
return result.run
}
func (r *goRunner) run(f func()) {
r.events <- f
}