Add M0 server skeleton with SQLite store, SSE events, worktrees, and tailscale service
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"g.fc.run/misc/byre/internal/api"
|
||||
)
|
||||
|
||||
type hub struct {
|
||||
mu sync.Mutex
|
||||
subs map[chan api.Event]bool
|
||||
}
|
||||
|
||||
func newHub() *hub {
|
||||
return &hub{subs: map[chan api.Event]bool{}}
|
||||
}
|
||||
|
||||
func (h *hub) subscribe() chan api.Event {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
ch := make(chan api.Event, 256)
|
||||
h.subs[ch] = true
|
||||
return ch
|
||||
}
|
||||
|
||||
func (h *hub) unsubscribe(ch chan api.Event) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if !h.subs[ch] {
|
||||
return
|
||||
}
|
||||
delete(h.subs, ch)
|
||||
close(ch)
|
||||
}
|
||||
|
||||
func (h *hub) publish(ev api.Event) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
for ch := range h.subs {
|
||||
select {
|
||||
case ch <- ev:
|
||||
default:
|
||||
delete(h.subs, ch)
|
||||
close(ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *hub) count() int {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return len(h.subs)
|
||||
}
|
||||
Reference in New Issue
Block a user