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 pathpage.go
201 lines (163 loc) · 4.27 KB
/
page.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
package golive
import (
"bytes"
"fmt"
"html/template"
)
var BasePage *template.Template
func init() {
var err error
BasePage, err = template.New("BasePage").Parse(BasePageString)
if err != nil {
panic(err)
}
}
type PageEnum struct {
EventLiveInput string
EventLiveMethod string
EventLiveDom string
EventLiveConnectElement string
EventLiveError string
DiffSetAttr DiffType
DiffRemoveAttr DiffType
DiffReplace DiffType
DiffRemove DiffType
DiffSetInnerHTML DiffType
DiffAppend DiffType
DiffMove DiffType
}
type LivePageEvent struct {
Type int
Component *LiveComponent
Source *EventSource
}
type LiveEventsChannel chan LivePageEvent
type Page struct {
content PageContent
Events LiveEventsChannel
ComponentsLifeCycle *ComponentLifeCycle
entryComponent *LiveComponent
// Components is a list that handle all the components from the page
Components map[string]*LiveComponent
}
type PageContent struct {
Lang string
Body template.HTML
Head template.HTML
Script string
Title string
Enum PageEnum
EnumLiveError map[string]string
}
func NewLivePage(c *LiveComponent) *Page {
componentsUpdatesChannel := make(ComponentLifeCycle)
pageEventsChannel := make(LiveEventsChannel)
return &Page{
entryComponent: c,
Events: pageEventsChannel,
ComponentsLifeCycle: &componentsUpdatesChannel,
Components: make(map[string]*LiveComponent),
}
}
func (lp *Page) SetContent(c PageContent) {
lp.content = c
}
// Call the Component in sequence of life cycle
func (lp *Page) Mount() {
// Enable components lifecycle channel receiver
lp.enableComponentLifeCycleReceiver()
// pass mount live Component with lifecycle channel
err := lp.entryComponent.Create(lp.ComponentsLifeCycle)
if err != nil {
panic(fmt.Errorf("mount: create entryComponent: %w", err))
}
err = lp.entryComponent.Mount()
if err != nil {
panic(err)
}
}
func (lp *Page) Render() (string, error) {
rendered, err := lp.entryComponent.Render()
if err != nil {
return "", err
}
// Body content
lp.content.Body = template.HTML(rendered)
lp.content.Enum = PageEnum{
EventLiveInput: EventLiveInput,
EventLiveMethod: EventLiveMethod,
EventLiveDom: EventLiveDom,
EventLiveError: EventLiveError,
EventLiveConnectElement: EventLiveConnectElement,
DiffSetAttr: SetAttr,
DiffRemoveAttr: RemoveAttr,
DiffReplace: Replace,
DiffRemove: Remove,
DiffSetInnerHTML: SetInnerHTML,
DiffAppend: Append,
DiffMove: Move,
}
lp.content.EnumLiveError = LiveErrorMap()
writer := bytes.NewBuffer([]byte{})
err = BasePage.Execute(writer, lp.content)
return writer.String(), err
}
func (lp *Page) Emit(lts int, c *LiveComponent) {
lp.EmitWithSource(lts, c, nil)
}
func (lp *Page) EmitWithSource(lts int, c *LiveComponent, source *EventSource) {
if c == nil {
c = lp.entryComponent
}
lp.Events <- LivePageEvent{
Type: lts,
Component: c,
Source: source,
}
}
func (lp *Page) HandleBrowserEvent(m BrowserEvent) error {
c := lp.entryComponent.findComponentByID(m.ComponentID)
if c == nil {
return fmt.Errorf("Component not found with id: %s", m.ComponentID)
}
var source *EventSource
var err error
switch m.Name {
case EventLiveInput:
err = c.SetValueInPath(m.StateValue, m.StateKey)
source = &EventSource{Type: EventSourceInput, Value: m.StateKey}
case EventLiveMethod:
err = c.InvokeMethodInPath(m.MethodName, m.MethodData, m.DOMEvent)
case EventLiveDisconnect:
err = c.Kill()
}
c.UpdateWithSource(source)
return err
}
const PageComponentUpdated = 1
const PageComponentMounted = 2
func (lp *Page) enableComponentLifeCycleReceiver() {
go func() {
for {
ls := <-*lp.ComponentsLifeCycle
switch ls.Stage {
case Created:
lp.Emit(PageComponentMounted, ls.Component)
break
case WillMount:
break
case Mounted:
break
case Updated:
lp.EmitWithSource(PageComponentUpdated, ls.Component, ls.Source)
break
case WillUnmount:
break
case Unmounted:
break
case Rendered:
break
}
}
}()
}