forked from myitcv/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.go
326 lines (246 loc) · 7.97 KB
/
react.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) 2016 Paul Jolly <paul@myitcv.org.uk>, all rights reserved.
// Use of this document is governed by a license found in the LICENSE document.
/*
Package react is a set of GopherJS bindings for Facebook's React, a Javascript
library for building user interfaces.
For more information see https://github.com/myitcv/react/wiki
*/
package react // import "myitcv.io/react"
//go:generate cssGen
//go:generate coreGen
import (
"fmt"
"reflect"
"honnef.co/go/js/dom"
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/jsbuiltin"
// imported for the side effect of bundling react
// build tags control whether this actually includes
// js files or not
_ "myitcv.io/react/internal/bundle"
"myitcv.io/react/internal/core"
)
const (
reactCompProps = "props"
reactCompLastState = "__lastState"
reactComponentBuilder = "__componentBuilder"
reactCompDisplayName = "displayName"
reactCompSetState = "setState"
reactCompForceUpdate = "forceUpdate"
reactCompState = "state"
reactCompGetInitialState = "getInitialState"
reactCompShouldComponentUpdate = "shouldComponentUpdate"
reactCompComponentDidMount = "componentDidMount"
reactCompComponentWillReceiveProps = "componentWillReceiveProps"
reactCompComponentWillMount = "componentWillMount"
reactCompComponentWillUnmount = "componentWillUnmount"
reactCompRender = "render"
reactCreateElement = "createElement"
reactCreateClass = "createClass"
reactDOMRender = "render"
nestedChildren = "_children"
nestedProps = "_props"
nestedState = "_state"
nestedComponentWrapper = "__ComponentWrapper"
)
var react = js.Global.Get("React")
var reactDOM = js.Global.Get("ReactDOM")
var object = js.Global.Get("Object")
var symbolFragment = react.Get("Fragment")
// ComponentDef is embedded in a type definition to indicate the type is a component
type ComponentDef struct {
elem *js.Object
}
var compMap = make(map[reflect.Type]*js.Object)
// S is the React representation of a string
type S = core.S
func Sprintf(format string, args ...interface{}) S {
return S(fmt.Sprintf(format, args...))
}
type elementHolder = core.ElementHolder
type Element = core.Element
type Component interface {
RendersElement() Element
}
type componentWithWillMount interface {
Component
ComponentWillMount()
}
type componentWithDidMount interface {
Component
ComponentDidMount()
}
type componentWithWillReceiveProps interface {
Component
ComponentWillReceivePropsIntf(i interface{})
}
type componentWithGetInitialState interface {
Component
GetInitialStateIntf() State
}
type componentWithWillUnmount interface {
Component
ComponentWillUnmount()
}
type Props interface {
IsProps()
EqualsIntf(v Props) bool
}
type State interface {
IsState()
EqualsIntf(v State) bool
}
func (c ComponentDef) Props() Props {
return *(unwrapValue(c.elem.Get(reactCompProps).Get(nestedProps)).(*Props))
}
func (c ComponentDef) Children() []Element {
v := c.elem.Get(reactCompProps).Get(nestedChildren)
if v == js.Undefined {
return nil
}
return *(unwrapValue(v).(*[]Element))
}
func (c ComponentDef) SetState(i State) {
rs := c.elem.Get(reactCompState)
is := rs.Get(nestedState)
cur := *(unwrapValue(is.Get(reactCompLastState)).(*State))
if i.EqualsIntf(cur) {
return
}
is.Set(reactCompLastState, wrapValue(&i))
c.elem.Call(reactCompForceUpdate)
}
func (c ComponentDef) State() State {
rs := c.elem.Get(reactCompState)
is := rs.Get(nestedState)
cur := *(unwrapValue(is.Get(reactCompLastState)).(*State))
return cur
}
func (c ComponentDef) ForceUpdate() {
c.elem.Call(reactCompForceUpdate)
}
type ComponentBuilder func(elem ComponentDef) Component
func CreateElement(buildCmp ComponentBuilder, newprops Props, children ...Element) Element {
cmp := buildCmp(ComponentDef{})
typ := reflect.TypeOf(cmp)
comp, ok := compMap[typ]
if !ok {
comp = buildReactComponent(typ, buildCmp)
compMap[typ] = comp
}
propsWrap := object.New()
if newprops != nil {
propsWrap.Set(nestedProps, wrapValue(&newprops))
}
if children != nil {
propsWrap.Set(nestedChildren, wrapValue(&children))
}
args := []interface{}{comp, propsWrap}
for _, v := range children {
args = append(args, v)
}
return &elementHolder{
Elem: react.Call(reactCreateElement, args...),
}
}
func createElement(cmp interface{}, props interface{}, children ...Element) Element {
args := []interface{}{cmp, props}
for _, v := range children {
args = append(args, v)
}
return &elementHolder{
Elem: react.Call(reactCreateElement, args...),
}
}
func buildReactComponent(typ reflect.Type, builder ComponentBuilder) *js.Object {
compDef := object.New()
compDef.Set(reactCompDisplayName, fmt.Sprintf("%v(%v)", typ.Name(), typ.PkgPath()))
compDef.Set(reactComponentBuilder, builder)
compDef.Set(reactCompGetInitialState, js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
elem := this
cmp := builder(ComponentDef{elem: elem})
var wv *js.Object
res := object.New()
is := object.New()
if cmp, ok := cmp.(componentWithGetInitialState); ok {
x := cmp.GetInitialStateIntf()
wv = wrapValue(&x)
}
res.Set(nestedState, is)
is.Set(reactCompLastState, wv)
return res
}))
compDef.Set(reactCompShouldComponentUpdate, js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
var nextProps Props
var curProps Props
// whether a component should update is only a function of its props
// ... and a component does not need to have props
//
// the only way we have of determining that here is whether the this
// object has a props property that has a non-nil nestedProps property
if this != nil {
if p := this.Get(reactCompProps); p != nil {
if ok, err := jsbuiltin.In(nestedProps, p); err == nil && ok {
if v := (p.Get(nestedProps)); v != nil {
curProps = *(unwrapValue(v).(*Props))
}
} else {
return false
}
}
}
if arguments[0] != nil {
if ok, err := jsbuiltin.In(nestedProps, arguments[0]); err == nil && ok {
nextProps = *(unwrapValue(arguments[0].Get(nestedProps)).(*Props))
}
}
return !curProps.EqualsIntf(nextProps)
}))
compDef.Set(reactCompComponentDidMount, js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
elem := this
cmp := builder(ComponentDef{elem: elem})
if cmp, ok := cmp.(componentWithDidMount); ok {
cmp.ComponentDidMount()
}
return nil
}))
compDef.Set(reactCompComponentWillReceiveProps, js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
elem := this
cmp := builder(ComponentDef{elem: elem})
if cmp, ok := cmp.(componentWithWillReceiveProps); ok {
ourProps := *(unwrapValue(arguments[0].Get(nestedProps)).(*Props))
cmp.ComponentWillReceivePropsIntf(ourProps)
}
return nil
}))
compDef.Set(reactCompComponentWillUnmount, js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
elem := this
cmp := builder(ComponentDef{elem: elem})
if cmp, ok := cmp.(componentWithWillUnmount); ok {
cmp.ComponentWillUnmount()
}
return nil
}))
compDef.Set(reactCompComponentWillMount, js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
elem := this
cmp := builder(ComponentDef{elem: elem})
// TODO we can make this more efficient by not doing the type check
// within the function body; it is known at the time of setting
// "componentWillMount" on the compDef
if cmp, ok := cmp.(componentWithWillMount); ok {
cmp.ComponentWillMount()
}
return nil
}))
compDef.Set(reactCompRender, js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
elem := this
cmp := builder(ComponentDef{elem: elem})
renderRes := cmp.RendersElement()
return renderRes
}))
return react.Call(reactCreateClass, compDef)
}
func Render(el Element, container dom.Element) Element {
v := reactDOM.Call(reactDOMRender, el, container)
return &elementHolder{Elem: v}
}