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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
package tui
import (
"fmt"
"reflect"
"sync"
)
type Events[T any] struct {
mu sync.RWMutex
subscribers []*eventSubscriber[T]
app *App
topic string
eventType reflect.Type
}
type eventSubscriber[T any] struct {
fn func(T)
active bool
unsubscribe func()
}
type topicSubscription struct {
eventType reflect.Type
listeners map[uint64]func(any)
nextID uint64
}
func NewEvents[T any](topic string) *Events[T] {
if topic == "" {
panic("tui: empty topic in NewEvents")
}
return &Events[T]{topic: topic, eventType: eventTypeOf[T]()}
}
func NewEventsForApp[T any](app *App, topic string) *Events[T] {
if app == nil {
panic("tui: nil app in NewEventsForApp")
}
ev := NewEvents[T](topic)
ev.BindApp(app)
return ev
}
func (e *Events[T]) BindApp(app *App) {
if app == nil {
panic("tui: nil app in Events.BindApp")
}
e.mu.Lock()
defer e.mu.Unlock()
if e.app == app {
e.bindSubscribersLocked(app)
return
}
e.unbindSubscribersLocked()
e.app = app
e.bindSubscribersLocked(app)
}
func (e *Events[T]) UnbindApp() {
e.mu.Lock()
defer e.mu.Unlock()
e.unbindSubscribersLocked()
e.app = nil
}
func (e *Events[T]) Emit(event T) {
app := e.resolveApp()
app.publishTopic(e.topic, e.eventType, event)
app.MarkDirty()
}
func (e *Events[T]) Subscribe(fn func(T)) func() {
if fn == nil {
return func() {}
}
e.mu.Lock()
sub := &eventSubscriber[T]{fn: fn, active: true}
e.subscribers = append(e.subscribers, sub)
if e.app != nil {
unsub, err := e.app.subscribeTopic(e.topic, e.eventType, func(v any) {
sub.fn(v.(T))
})
if err == nil {
sub.unsubscribe = unsub
}
}
e.mu.Unlock()
return func() {
e.mu.Lock()
defer e.mu.Unlock()
if !sub.active {
return
}
sub.active = false
if sub.unsubscribe != nil {
sub.unsubscribe()
sub.unsubscribe = nil
}
}
}
func (e *Events[T]) resolveApp() *App {
e.mu.RLock()
app := e.app
e.mu.RUnlock()
if app != nil {
return app
}
panic("tui.Events used without app context; call BindApp or use NewEventsForApp")
}
func (e *Events[T]) bindSubscribersLocked(app *App) {
for _, sub := range e.subscribers {
if !sub.active || sub.unsubscribe != nil {
continue
}
unsub, err := app.subscribeTopic(e.topic, e.eventType, func(v any) {
sub.fn(v.(T))
})
if err == nil {
sub.unsubscribe = unsub
}
}
}
func (e *Events[T]) unbindSubscribersLocked() {
for _, sub := range e.subscribers {
if sub.unsubscribe != nil {
sub.unsubscribe()
sub.unsubscribe = nil
}
}
}
func (a *App) subscribeTopic(topic string, eventType reflect.Type, fn func(any)) (func(), error) {
if a == nil {
panic("tui: nil app in subscribeTopic")
}
if topic == "" {
panic("tui: empty topic in subscribeTopic")
}
if fn == nil {
return func() {}, nil
}
a.topicMu.Lock()
if a.topics == nil {
a.topics = make(map[string]*topicSubscription)
}
sub, exists := a.topics[topic]
if !exists {
sub = &topicSubscription{
eventType: eventType,
listeners: make(map[uint64]func(any)),
}
a.topics[topic] = sub
} else if sub.eventType != eventType {
a.topicMu.Unlock()
return nil, fmt.Errorf("tui: topic type mismatch for %s: existing=%s, new=%s", topic, sub.eventType, eventType)
}
id := sub.nextID
sub.nextID++
sub.listeners[id] = fn
a.topicMu.Unlock()
removed := false
return func() {
a.topicMu.Lock()
defer a.topicMu.Unlock()
if removed {
return
}
removed = true
if sub, ok := a.topics[topic]; ok {
delete(sub.listeners, id)
}
}, nil
}
func (a *App) publishTopic(topic string, eventType reflect.Type, event any) {
if a == nil {
panic("tui: nil app in publishTopic")
}
if topic == "" {
panic("tui: empty topic in publishTopic")
}
a.topicMu.RLock()
sub, ok := a.topics[topic]
if !ok {
a.topicMu.RUnlock()
return
}
if sub.eventType != eventType {
a.topicMu.RUnlock()
return
}
listeners := make([]func(any), 0, len(sub.listeners))
for _, fn := range sub.listeners {
listeners = append(listeners, fn)
}
a.topicMu.RUnlock()
for _, fn := range listeners {
fn(event)
}
}
func eventTypeOf[T any]() reflect.Type {
return reflect.TypeFor[T]()
}
|