Files
llink/go/internal/pusher/channel.go
T

58 lines
1.2 KiB
Go

package pusher
// Channel tracks the local connections subscribed on this pod.
// State is only mutated by the Hub goroutine, so no locks are needed.
type Channel struct {
id string
members map[*Conn]string // conn → humanID
}
func newChannel(id string) *Channel {
return &Channel{
id: id,
members: make(map[*Conn]string),
}
}
func (ch *Channel) addMember(conn *Conn, humanID string) {
ch.members[conn] = humanID
}
func (ch *Channel) removeMember(conn *Conn) {
delete(ch.members, conn)
}
func (ch *Channel) isEmpty() bool {
return len(ch.members) == 0
}
// Deduplicated set; the same human may have multiple connections.
func (ch *Channel) localHumanIDs() []string {
seen := make(map[string]bool, len(ch.members))
ids := make([]string, 0, len(ch.members))
for _, hid := range ch.members {
if !seen[hid] {
seen[hid] = true
ids = append(ids, hid)
}
}
return ids
}
func (ch *Channel) hasHumanID(humanID string) bool {
for _, hid := range ch.members {
if hid == humanID {
return true
}
}
return false
}
func (ch *Channel) broadcast(msg ServerMessage, exclude *Conn) {
for conn := range ch.members {
if conn != exclude {
conn.Send(msg)
}
}
}