-
Notifications
You must be signed in to change notification settings - Fork 5
/
event.go
70 lines (61 loc) · 1.73 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
61
62
63
64
65
66
67
68
69
70
package fir
import (
"fmt"
"time"
"github.com/goccy/go-json"
"github.com/livefir/fir/internal/logger"
)
// NewEvent creates a new event
func NewEvent(id string, params any) Event {
data, err := json.Marshal(params)
if err != nil {
logger.Errorf("error marshaling event params: %v, %v, %v ,", id, params, err)
return Event{
ID: id,
}
}
return Event{
ID: id,
Params: data,
}
}
func toUnixTime(ts int64) time.Time {
return time.Unix(ts/1000, (ts%1000)*1000*1000)
}
// Event is a struct that holds the data for an incoming user event
type Event struct {
// ID is the event id
ID string `json:"event_id"`
// Params is the data to be passed to the event
Params json.RawMessage `json:"params"`
// Target is the dom element id which emitted the event. The DOM events generated by the event will be targeted to this element.
Target *string `json:"target,omitempty"`
// IsForm is a boolean that indicates whether the event was triggered by a form submission
IsForm bool `json:"is_form,omitempty"`
// SessionID is the id of the session that the event was triggered for
SessionID *string `json:"session_id,omitempty"`
ElementKey *string `json:"element_key,omitempty"`
Timestamp int64 `json:"ts,omitempty"`
}
// String returns the string representation of the event
func (e Event) String() string {
data, _ := json.MarshalIndent(e, "", " ")
return string(data)
}
func fir(parts ...string) *string {
if len(parts) == 0 {
panic("fir: fir() called with no arguments")
}
if len(parts) == 1 {
s := fmt.Sprintf("fir:%s", parts[0])
return &s
}
if len(parts) == 2 {
s := fmt.Sprintf("fir:%s::%s", parts[0], parts[1])
return &s
}
if len(parts) > 2 {
panic("fir: fir() called with more than 2 arguments")
}
return nil
}