-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcallbacks.go
273 lines (219 loc) · 7.39 KB
/
callbacks.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
/*
Copyright 2015 Palm Stone Games, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package polymer
import (
"fmt"
"reflect"
"strings"
"github.com/gopherjs/gopherjs/js"
)
var (
typeOfPtrProto = reflect.TypeOf(&Proto{})
typeOfJsObject = reflect.TypeOf(&js.Object{})
)
func createdCallback(refType reflect.Type) *js.Object {
return js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
// Create a new Go side object
refVal := reflect.New(refType.Elem())
proto := refVal.Interface().(Interface)
refVal = refVal.Elem()
// Set the proto value, this is needed because we get our callers to embed *polymer.Proto, and it needs to get instantiated
refVal.FieldByName("Proto").Set(reflect.ValueOf(&Proto{}))
// Store ourselves in js land so we can map js to proto
jsMap = append(jsMap, proto)
this.Set(protoIndexKey, len(jsMap)-1)
// Set data on the proto
data := proto.data()
data.this = this
data.Element = WrapJSElement(this)
// Setup channel based event handlers
for _, handler := range parseChanHandlers(refType) {
// Create channel
chanVal := refVal.FieldByIndex(handler.Index)
chanVal.Set(reflect.MakeChan(chanVal.Type(), 0))
// Set handler function
this.Set(getJsName(handler.Name), eventChanCallback(chanVal))
}
// Call the proto side callback for user hooks
proto.Created()
return nil
})
}
func ensureReady(proto Interface) {
refVal := reflect.ValueOf(proto).Elem()
refType := reflect.TypeOf(proto).Elem()
data := proto.data()
if data.ready {
return
}
data.ready = true
// Set initial field values
for i := 0; i < refType.NumField(); i++ {
// Get field info first
fieldVal := refVal.Field(i)
fieldType := refType.Field(i)
jsName := getJsName(fieldType.Name)
// Ignore the *Proto and *BindProto anonymous field
if fieldType.Anonymous && (fieldType.Type == typeOfPtrProto || fieldType.Type == typeOfPtrBindProto) {
continue
}
// Special case check to not overwrite Model on dom-bind templates
if _, ok := proto.(*autoBindTemplate); ok && fieldType.Name == "Model" {
continue
}
// Skip unexported fields
if !isFieldExported(fieldType.Name) {
continue
}
// If the value in JS is set, we take it over
// Otherwise, we take over the (usually zeroed) go value and set it in JS
// We can get away with doing this for only first level values, as they'll either get decoded recursively if they were set
// Or they'll get set from Go in their entirety if they were undefined
if fieldVal.Kind() != reflect.Chan {
jsVal := data.this.Get(jsName)
if jsVal == nil || jsVal == js.Undefined {
jsObj, filled := encodeRaw(fieldVal)
if filled {
proto.data().doNotify(jsName, jsObj)
}
} else {
currVal := reflect.New(fieldType.Type)
if err := Decode(jsVal, currVal.Interface()); err != nil {
panic(fmt.Sprintf("Error while decoding polymer field value for %v: %v", fieldType.Name, err))
}
fieldVal.Set(currVal.Elem())
}
}
}
}
func readyCallback() *js.Object {
return js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
// Lookup the proto
proto := lookupProto(this)
ensureReady(proto)
proto.Ready()
return nil
})
}
func attachedCallback() *js.Object {
return js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
// Lookup the proto
proto := lookupProto(this)
// Call the proto side callback for user hooks
proto.Attached()
return nil
})
}
func detachedCallback() *js.Object {
return js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
// Lookup the proto
proto := lookupProto(this)
// Call the proto side callback for user hooks
proto.Detached()
return nil
})
}
func observeShallowCallback(path []string) *js.Object {
return js.MakeFunc(func(this *js.Object, jsArgs []*js.Object) interface{} {
setObservedValue(lookupProto(this), path, jsArgs[0])
return nil
})
}
func observeDeepCallback() *js.Object {
return js.MakeFunc(func(this *js.Object, jsArgs []*js.Object) interface{} {
record := jsArgs[0]
setObservedValue(lookupProto(this), strings.Split(record.Get("path").String(), "."), record.Get("value"))
return nil
})
}
// reflectArgs builds up reflect args
// We loop through the function arguments and use the types of each argument to decode the jsArgs
// If the function has more arguments than we have jsArgs, they're passed in as Zero values
// If the function has less arguments than jsArgs, the superfluous jsArgs are silently discarded
func reflectArgs(handler reflect.Value, proto interface{}, jsArgs []*js.Object) ([]reflect.Value, error) {
handlerType := handler.Type()
reflectArgs := make([]reflect.Value, handlerType.NumIn())
jsIndex := 0
for goIndex := 0; goIndex < handlerType.NumIn(); goIndex++ {
argType := handlerType.In(goIndex)
if goIndex == 0 && len(reflectArgs) != 0 && argType == reflect.TypeOf(proto) {
reflectArgs[goIndex] = reflect.ValueOf(proto)
} else {
argPtrVal := reflect.New(argType)
if len(jsArgs) > jsIndex {
if err := decodeRaw(jsArgs[jsIndex], argPtrVal.Elem()); err != nil {
return nil, err
}
}
reflectArgs[goIndex] = argPtrVal.Elem()
jsIndex++
}
}
return reflectArgs, nil
}
func eventHandlerCallback(handler reflect.Value) *js.Object {
return js.MakeFunc(func(this *js.Object, jsArgs []*js.Object) interface{} {
proto := lookupProto(this)
jsArgs[0] = js.Global.Get("Polymer").Call("dom", jsArgs[0])
var args []reflect.Value
var err error
if autoBind, ok := proto.(*autoBindTemplate); ok {
args, err = reflectArgs(handler, autoBind.Model, jsArgs)
} else {
args, err = reflectArgs(handler, proto, jsArgs)
}
if err != nil {
Log("Suppressed event ", jsArgs[0], " due to error while decoding: ", err.Error())
return nil
}
handler.Call(args)
return nil
})
}
func eventChanCallback(handlerChan reflect.Value) *js.Object {
chanArgType := handlerChan.Type().Elem()
return js.MakeFunc(func(this *js.Object, jsArgs []*js.Object) interface{} {
chanArg := reflect.New(chanArgType)
if err := decodeRaw(js.Global.Get("Polymer").Call("dom", jsArgs[0]), chanArg.Elem()); err != nil {
Log("Suppressed event ", jsArgs[0], " due to error while decoding: ", err.Error())
return nil
}
go func() {
handlerChan.Send(chanArg.Elem())
}()
return nil
})
}
func computeCallback(handler reflect.Value) *js.Object {
return js.MakeFunc(func(this *js.Object, jsArgs []*js.Object) interface{} {
proto := lookupProto(this)
ensureReady(proto)
var (
returnArgs []reflect.Value
args []reflect.Value
err error
)
if autoBind, ok := proto.(*autoBindTemplate); ok {
args, err = reflectArgs(handler, autoBind.Model, jsArgs)
} else {
args, err = reflectArgs(handler, proto, jsArgs)
}
if err != nil {
Log("Suppressed compute function %v due to error while decoding: %v", handler, err)
return nil
}
returnArgs = handler.Call(args)
encodedReturn, _ := encodeRaw(returnArgs[0])
return encodedReturn
})
}