This repository has been archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsession.go
203 lines (162 loc) · 4.38 KB
/
session.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package golive
import (
"fmt"
"strings"
)
const (
EventLiveInput = "li"
EventLiveMethod = "lm"
EventLiveDom = "ld"
EventLiveDisconnect = "lx"
EventLiveError = "le"
EventLiveConnectElement = "lce"
)
var (
LiveErrorSessionNotFound = "session_not_found"
)
func LiveErrorMap() map[string]string {
return map[string]string{
"LiveErrorSessionNotFound": LiveErrorSessionNotFound,
}
}
type BrowserEvent struct {
Name string `json:"name"`
ComponentID string `json:"component_id"`
MethodName string `json:"method_name"`
MethodData map[string]string `json:"method_data"`
StateKey string `json:"key"`
StateValue string `json:"value"`
DOMEvent *DOMEvent `json:"dom_event"`
}
type DOMEvent struct {
KeyCode string `json:"keyCode"`
}
type SessionStatus string
const (
SessionNew SessionStatus = "n"
SessionOpen SessionStatus = "o"
SessionClosed SessionStatus = "c"
)
type Session struct {
LivePage *Page
OutChannel chan PatchBrowser
log Log
Status SessionStatus
}
func NewSession() *Session {
return &Session{
OutChannel: make(chan PatchBrowser),
Status: SessionNew,
}
}
func (s *Session) QueueMessage(message PatchBrowser) {
go func() {
s.OutChannel <- message
}()
}
func (s *Session) IngestMessage(message BrowserEvent) error {
defer func() {
payload := recover()
if payload != nil {
// TODO: get session key in log
s.log(LogWarn, fmt.Sprintf("ingest message: recover from panic: %v", payload), logEx{"message": message})
}
}()
err := s.LivePage.HandleBrowserEvent(message)
if err != nil {
return err
}
return nil
}
func (s *Session) ActivatePage(lp *Page) {
s.LivePage = lp
// Here is the location that get all the components updates *notified* by
// the page!
go func() {
for {
// Receive all the events from page
evt := <-s.LivePage.Events
s.log(LogDebug, fmt.Sprintf("Component %s triggering %d", evt.Component.Name, evt.Type), logEx{"evt": evt})
switch evt.Type {
case PageComponentUpdated:
if err := s.LiveRenderComponent(evt.Component, evt.Source); err != nil {
s.log(LogError, "entryComponent live render", logEx{"error": err})
}
break
case PageComponentMounted:
s.QueueMessage(PatchBrowser{
ComponentID: evt.Component.Name,
Type: EventLiveConnectElement,
Instructions: nil,
})
break
}
}
}()
}
func (s *Session) generateBrowserPatchesFromDiff(diff *diff, source *EventSource) ([]*PatchBrowser, error) {
bp := make([]*PatchBrowser, 0)
for _, instruction := range diff.instructions {
selector, err := selectorFromNode(instruction.element)
if skipUpdateValueOnInput(instruction, source) {
continue
}
if err != nil {
return nil, fmt.Errorf("selector from node: %w instruction: %v", err, instruction)
}
componentID, err := componentIDFromNode(instruction.element)
if err != nil {
return nil, err
}
var patch *PatchBrowser
// find if there is already a patch
for _, pb := range bp {
if pb.ComponentID == componentID {
patch = pb
break
}
}
// If there is no patch
if patch == nil {
patch = NewPatchBrowser(componentID)
patch.Type = EventLiveDom
bp = append(bp, patch)
}
patch.AddInstruction(PatchInstruction{
Name: EventLiveDom,
Type: instruction.changeType.toString(),
Attr: map[string]string{
"Name": instruction.attr.name,
"Value": instruction.attr.value,
},
Index: instruction.index,
Content: instruction.content,
Selector: selector.toString(),
})
}
return bp, nil
}
func skipUpdateValueOnInput(in changeInstruction, source *EventSource) bool {
if in.element == nil || source == nil || in.changeType != SetAttr || strings.ToLower(in.attr.name) != "value" {
return false
}
attr := getAttribute(in.element, "go-live-input")
return attr != nil && source.Type == EventSourceInput && attr.Val == source.Value
}
// LiveRenderComponent render the updated Component and compare with
// last state. It may apply with *all child components*
func (s *Session) LiveRenderComponent(c *LiveComponent, source *EventSource) error {
var err error
diff, err := c.LiveRender()
if err != nil {
return err
}
patches, err := s.generateBrowserPatchesFromDiff(diff, source)
if err != nil {
return err
}
for _, om := range patches {
s.QueueMessage(*om)
}
return nil
}