-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_subscription.go
51 lines (38 loc) · 785 Bytes
/
client_subscription.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
// Copyright (c) Omlox Client Go Contributors
// SPDX-License-Identifier: MIT
package omlox
import (
"encoding/json"
)
const (
receiveChanSize = 256
)
// Subcription represents a topic subscription to the websocket Hub interface.
type Subcription struct {
sid int
topic Topic
params Parameters
mch chan *WrapperObject
}
func ReceiveAs[T any](sub *Subcription) <-chan *T {
out := make(chan *T, receiveChanSize)
go func() {
defer close(out)
for msg := range sub.mch {
for _, payload := range msg.Payload {
var v T
if err := json.Unmarshal(payload, &v); err != nil {
continue
}
out <- &v
}
}
}()
return out
}
func (s Subcription) ReceiveRaw() <-chan *WrapperObject {
return s.mch
}
func (s *Subcription) close() {
close(s.mch)
}