-
Notifications
You must be signed in to change notification settings - Fork 3
/
subscription_event.go
46 lines (37 loc) · 1.09 KB
/
subscription_event.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
package actioncable
import (
"encoding/json"
)
type SubscriptionEvent struct {
Type SubscriptionEventType
Message map[string]interface{}
RawMessage *json.RawMessage
}
type SubscriptionEventType string
const (
Connected SubscriptionEventType = SubscriptionEventType("connected")
Disconnected SubscriptionEventType = SubscriptionEventType("disconnected")
Rejected SubscriptionEventType = SubscriptionEventType("rejected")
Received SubscriptionEventType = SubscriptionEventType("received")
)
func createSubscriptionEvent(eventType SubscriptionEventType, event *Event) *SubscriptionEvent {
if event == nil {
event = NewNilEvent()
}
e := newSubscriptionEvent(eventType, &event.Message)
return e
}
func newSubscriptionEvent(eventType SubscriptionEventType, raw *json.RawMessage) *SubscriptionEvent {
return &SubscriptionEvent{
Type: eventType,
Message: map[string]interface{}{},
RawMessage: raw,
}
}
func (s *SubscriptionEvent) ReadJSON(v interface{}) error {
b, err := json.Marshal(s.RawMessage)
if err != nil {
return err
}
return json.Unmarshal(b, v)
}