-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathevent.go
60 lines (53 loc) · 1.34 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package live
import (
"encoding/json"
)
// EventConfig configures an event.
type EventConfig func(e *Event) error
const (
// EventError indicates an error has occurred.
EventError = "err"
// EventPatch a patch event containing a diff.
EventPatch = "patch"
// EventAck sent when an event is acknowledged.
EventAck = "ack"
// EventConnect sent as soon as the server accepts the
// WS connection.
EventConnect = "connect"
// EventParams sent for a URL parameter update. Can be
// sent both directions.
EventParams = "params"
// EventRedirect sent in order to trigger a browser
// redirect.
EventRedirect = "redirect"
)
// Event messages that are sent and received by the
// socket.
type Event struct {
T string `json:"t"`
ID int `json:"i,omitempty"`
Data json.RawMessage `json:"d,omitempty"`
SelfData interface{} `json:"s,omitempty"`
}
// Params extract params from inbound message.
func (e Event) Params() (Params, error) {
if e.Data == nil {
return Params{}, nil
}
var p Params
if err := json.Unmarshal(e.Data, &p); err != nil {
return nil, ErrMessageMalformed
}
return p, nil
}
// WithID sets an ID on an event.
func WithID(ID int) EventConfig {
return func(e *Event) error {
e.ID = ID
return nil
}
}
type ErrorEvent struct {
Source Event `json:"source"`
Err string `json:"err"`
}