-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
142 lines (129 loc) · 2.93 KB
/
view.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
package magic
import (
"encoding/json"
"fmt"
"io"
"log"
"github.com/Instantan/template"
)
type Template = template.Template
type ViewFn func(s Socket) AppliedView
type AppliedView struct {
ref *ref
template *Template
}
type Views = []AppliedView
type htmlRenderConfig struct {
static bool
}
func View(templ string) ViewFn {
t := template.Parse(injectLiveScript(templ))
return func(s Socket) AppliedView {
return AppliedView{
ref: s.(*ref),
template: t,
}
}
}
func (av AppliedView) html(w io.Writer, config *htmlRenderConfig) (n int, err error) {
av.template.Execute(w, func(w io.Writer, tag string) (int, error) {
if tag == "magic:inject" {
if config != nil && config.static {
return w.Write([]byte{})
}
return w.Write(magicMinScriptWithTags)
}
av.ref.assigning.Lock()
rv, ok := av.ref.state[tag]
av.ref.assigning.Unlock()
if !ok {
return 0, nil
}
switch v := rv.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, bool:
return w.Write(unsafeStringToBytes(fmt.Sprint(v)))
case string:
return w.Write(unsafeStringToBytes(v))
case AppliedView:
return v.html(w, config)
case []AppliedView:
n := 0
for i := range v {
m, err := v[i].html(w, config)
if err != nil {
log.Println(err)
}
n += m
}
return n, nil
case *[]AppliedView:
n := 0
for i := range *v {
m, err := (*v)[i].html(w, config)
if err != nil {
log.Println(err)
}
n += m
}
return n, nil
default:
return 0, nil
}
})
return n, err
}
func (av AppliedView) assignments() []*assignment {
psByref := map[uintptr]*assignment{}
av.assignment(&psByref)
ps := make([]*assignment, len(psByref)+1)
ps[0] = getAssignment()
ps[0].socketid = socketid(av.ref.root.id())
ps[0].data = map[string]any{
"#": AppliedView{
ref: av.ref,
template: av.template,
},
}
i := 1
for k := range psByref {
ps[i] = psByref[k]
i++
}
return ps
}
func (av AppliedView) assignment(assignmentsByref *map[uintptr]*assignment) {
refid := av.ref.id()
if _, ok := (*assignmentsByref)[refid]; ok {
return
}
p := getAssignment()
p.data = av.ref.state
p.socketid = socketid(refid)
(*assignmentsByref)[refid] = p
for _, d := range p.data {
switch v := d.(type) {
case AppliedView:
v.assignment(assignmentsByref)
case []AppliedView:
for i := range v {
v[i].assignment(assignmentsByref)
}
case *[]AppliedView:
for i := range *v {
(*v)[i].assignment(assignmentsByref)
}
}
}
}
func (av AppliedView) MarshalJSON() ([]byte, error) {
d := make([]json.RawMessage, 2)
d[0] = socketid(av.ref.id())
d[1], _ = json.Marshal(av.template.ID())
return json.Marshal(d)
}
func (av AppliedView) marshalAssignmentJSON() ([]byte, error) {
m := make([]json.RawMessage, 2)
m[0], _ = json.Marshal(av.template.ID())
m[1], _ = json.Marshal(av.template.String())
return json.Marshal(m)
}