255e8dcf71
Frontend C++ ImGui (main.cpp + 4 paneles) + backend Go (HTTP + SQLite + fsnotify + SSE). Reusa parse/scan/watch funcs del registry (issue 0130a).
47 lines
731 B
Go
47 lines
731 B
Go
package main
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type SSEEvent struct {
|
|
Type string `json:"type"`
|
|
ID string `json:"id"`
|
|
Path string `json:"path,omitempty"`
|
|
}
|
|
|
|
type SSEHub struct {
|
|
mu sync.RWMutex
|
|
clients map[chan SSEEvent]struct{}
|
|
}
|
|
|
|
func newSSEHub() *SSEHub {
|
|
return &SSEHub{clients: map[chan SSEEvent]struct{}{}}
|
|
}
|
|
|
|
func (h *SSEHub) subscribe() chan SSEEvent {
|
|
ch := make(chan SSEEvent, 16)
|
|
h.mu.Lock()
|
|
h.clients[ch] = struct{}{}
|
|
h.mu.Unlock()
|
|
return ch
|
|
}
|
|
|
|
func (h *SSEHub) unsubscribe(ch chan SSEEvent) {
|
|
h.mu.Lock()
|
|
delete(h.clients, ch)
|
|
h.mu.Unlock()
|
|
close(ch)
|
|
}
|
|
|
|
func (h *SSEHub) broadcast(ev SSEEvent) {
|
|
h.mu.RLock()
|
|
defer h.mu.RUnlock()
|
|
for ch := range h.clients {
|
|
select {
|
|
case ch <- ev:
|
|
default:
|
|
}
|
|
}
|
|
}
|