forked from krisbuist/timeular-zei-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.go
57 lines (47 loc) · 958 Bytes
/
hub.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
package timeular
import "time"
type Client struct {
hub *Hub
send chan Timeular
}
type Hub struct {
clients map[*Client]bool
broadcast chan Timeular
register chan *Client
lastState Timeular
}
func NewHub() *Hub {
return &Hub{
broadcast: make(chan Timeular),
register: make(chan *Client),
clients: make(map[*Client]bool),
}
}
func (h *Hub) Register(send chan Timeular) {
c := &Client{send: send}
h.register <- c
}
func (h *Hub) Broadcast(t Timeular) {
h.broadcast <- t
}
func (h *Hub) Run() {
var debounceState <-chan time.Time
for {
select {
case client := <-h.register:
h.clients[client] = true
if !h.lastState.Tracking.StartedAt.IsZero() {
client.send <- h.lastState
}
case message := <-h.broadcast:
h.lastState = message
debounceState = time.After(2 * time.Second)
case <-debounceState:
for client := range h.clients {
select {
case client.send <- h.lastState:
}
}
}
}
}