Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

knotclient: improve api with NewConsumerConfig and AddEventSource

Signed-off-by: Anirudh Oppiliappan <anirudh@tangled.sh>

+17 -7
+17 -7
knotclient/events.go
··· 17 17 "github.com/gorilla/websocket" 18 18 ) 19 19 20 - type ProcessFunc func(source EventSource, message Message) error 20 + type ProcessFunc func(ctx context.Context, source EventSource, message Message) error 21 21 22 22 type Message struct { 23 23 Rkey string ··· 39 39 CursorStore CursorStore 40 40 } 41 41 42 + func NewConsumerConfig() *ConsumerConfig { 43 + return &ConsumerConfig{ 44 + Sources: make(map[EventSource]struct{}), 45 + } 46 + } 47 + 48 + func (cc *ConsumerConfig) AddEventSource(es EventSource) { 49 + cc.Sources[es] = struct{}{} 50 + } 51 + 42 52 type EventSource struct { 43 53 Knot string 44 54 } ··· 60 50 } 61 51 62 52 type EventConsumer struct { 63 - cfg ConsumerConfig 64 53 wg sync.WaitGroup 65 54 dialer *websocket.Dialer 66 55 connMap sync.Map ··· 67 58 logger *slog.Logger 68 59 randSource *rand.Rand 69 60 70 - // rw lock over edits to consumer config 71 - mu sync.RWMutex 61 + // rw lock over edits to ConsumerConfig 62 + cfgMu sync.RWMutex 63 + cfg ConsumerConfig 72 64 } 73 65 74 66 type CursorStore interface { ··· 212 202 } 213 203 214 204 func (c *EventConsumer) AddSource(ctx context.Context, s EventSource) { 215 - c.mu.Lock() 205 + c.cfgMu.Lock() 216 206 c.cfg.Sources[s] = struct{}{} 217 207 c.wg.Add(1) 218 208 go c.startConnectionLoop(ctx, s) 219 - c.mu.Unlock() 209 + c.cfgMu.Unlock() 220 210 } 221 211 222 212 func (c *EventConsumer) worker(ctx context.Context) { ··· 240 230 // update cursor 241 231 c.cfg.CursorStore.Set(j.source.Knot, time.Now().Unix()) 242 232 243 - if err := c.cfg.ProcessFunc(j.source, msg); err != nil { 233 + if err := c.cfg.ProcessFunc(ctx, j.source, msg); err != nil { 244 234 c.logger.Error("error processing message", "source", j.source, "err", err) 245 235 } 246 236 }