package subscriptions
import (
"fmt"
"ganigeorgiev.com/pocketbase/pocketbase/tools/list"
"ganigeorgiev.com/pocketbase/pocketbase/tools/store"
)
type Broker struct {
store *store.Store[string, Client]
}
func NewBroker() *Broker {
return &Broker{
store: store.New[string, Client](nil),
}
}
func (b *Broker) Clients() map[string]Client {
return b.store.GetAll()
}
func (b *Broker) ChunkedClients(chunkSize int) [][]Client {
return list.ToChunks(b.store.Values(), chunkSize)
}
func (b *Broker) TotalClients() int {
return b.store.Length()
}
func (b *Broker) ClientById(clientId string) (Client, error) {
client, ok := b.store.GetOk(clientId)
if !ok {
return nil, fmt.Errorf("no client associated with connection ID %q", clientId)
}
return client, nil
}
func (b *Broker) Register(client Client) {
b.store.Set(client.Id(), client)
}
func (b *Broker) Unregister(clientId string) {
client := b.store.Get(clientId)
if client == nil {
return
}
client.Discard()
b.store.Remove(clientId)
}