repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

pico / pkg / pubsub
Eric Bower  ·  2026-01-25

client.go

 1package pubsub
 2
 3import (
 4	"io"
 5	"iter"
 6	"sync"
 7
 8	"github.com/antoniomika/syncmap"
 9)
10
11func NewClient(ID string, rw io.ReadWriter, direction ChannelDirection, blockWrite, replay, keepAlive bool) *Client {
12	return &Client{
13		ID:         ID,
14		ReadWriter: rw,
15		Direction:  direction,
16		Channels:   syncmap.New[string, *Channel](),
17		Done:       make(chan struct{}),
18		Data:       make(chan ChannelMessage),
19		Replay:     replay,
20		BlockWrite: blockWrite,
21		KeepAlive:  keepAlive,
22	}
23}
24
25/*
26Client is the container for holding state between multiple devices.  A
27client has a direction (input, output, inputout) as well as a way to
28send data to all the associated channels.
29*/
30type Client struct {
31	ID         string
32	ReadWriter io.ReadWriter
33	Channels   *syncmap.Map[string, *Channel]
34	Direction  ChannelDirection
35	Done       chan struct{}
36	Data       chan ChannelMessage
37	Replay     bool
38	BlockWrite bool
39	KeepAlive  bool
40	once       sync.Once
41	onceData   sync.Once
42}
43
44func (c *Client) GetChannels() iter.Seq2[string, *Channel] {
45	return c.Channels.Range
46}
47
48func (c *Client) Cleanup() {
49	c.once.Do(func() {
50		close(c.Done)
51	})
52}