-
Notifications
You must be signed in to change notification settings - Fork 13
/
event.go
44 lines (36 loc) · 961 Bytes
/
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
package oplog
import (
"fmt"
"io"
"time"
)
// GenericEvent is an interface used by the oplog to send different kinds of
// SSE compatible events
type GenericEvent interface {
io.WriterTo
GetEventID() LastID
}
// genericLastID stores an arbitrary event id
type genericLastID string
// Event is used to send "technical" events with no data like "reset" or "live"
type Event struct {
ID string
Event string
}
// GetEventID returns an SSE event id
func (e Event) GetEventID() LastID {
i := genericLastID(e.ID)
return &i
}
// WriteTo serializes an event as a SSE compatible message
func (e Event) WriteTo(w io.Writer) (int64, error) {
n, err := fmt.Fprintf(w, "id: %s\nevent: %s\n\n", e.GetEventID(), e.Event)
return int64(n), err
}
func (gid genericLastID) String() string {
return string(gid)
}
// Time returns a zero time a GenericLastID doest not hold any time information
func (gid genericLastID) Time() time.Time {
return time.Time{}
}