-
Notifications
You must be signed in to change notification settings - Fork 175
/
events-dom.go
305 lines (250 loc) · 10.2 KB
/
events-dom.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
package vugu
import (
"sync"
"github.com/vugu/vugu/js"
)
// NewDOMEvent returns a new initialized DOMEvent.
func NewDOMEvent(eventEnv EventEnv, eventSummary map[string]interface{}) DOMEvent {
return &domEvent{
eventSummary: eventSummary,
eventEnv: eventEnv,
window: js.Global().Get("window"),
}
}
// DOMEvent is an event originated in the browser. It wraps the JS event that comes in.
// It is meant to be used in WebAssembly but some methods exist here so code can compile
// server-side as well (although DOMEvents should not ever be generated server-side).
type DOMEvent interface {
// Prop returns a value from the EventSummary using the keys you specify.
// The keys is a list of map keys to be looked up. For example:
// e.Prop("target", "name") will return the same value as e.EventSummary()["target"]["name"],
// except that Prop helps with some edge cases and if a value is missing
// of the wrong type, nil will be returned, instead of panicing.
Prop(keys ...string) interface{}
// PropString is like Prop but returns it's value as a string.
// No type conversion is done, if the requested value is not
// already a string then an empty string will be returned.
PropString(keys ...string) string
// PropFloat64 is like Prop but returns it's value as a float64.
// No type conversion is done, if the requested value is not
// already a float64 then float64(0) will be returned.
PropFloat64(keys ...string) float64
// PropBool is like Prop but returns it's value as a bool.
// No type conversion is done, if the requested value is not
// already a bool then false will be returned.
PropBool(keys ...string) bool
// EventSummary returns a map with simple properties (primitive types) from the event.
// Accessing values returns by EventSummary incurs no additional performance or memory
// penalty, whereas calls to JSEvent, JSEventTarget, etc. require a call into the browser
// JS engine and the attendant resource usage. So if you can get the information you
// need from the EventSummary, that's better.
EventSummary() map[string]interface{}
// JSEvent returns a js.Value in wasm that corresponds to the event object.
// Non-wasm implementation returns nil.
JSEvent() js.Value
// JSEventTarget returns the value of the "target" property of the event, the element
// that the event was originally fired/registered on.
JSEventTarget() js.Value
// JSEventCurrentTarget returns the value of the "currentTarget" property of the event, the element
// that is currently processing the event.
JSEventCurrentTarget() js.Value
// EventEnv returns the EventEnv for the current environment and allows locking and unlocking around modifications.
// See EventEnv struct.
EventEnv() EventEnv
// PreventDefault calls preventDefault() on the underlying DOM event.
// May only be used within event handler in same goroutine.
PreventDefault()
// StopPropagation calls stopPropagation() on the underlying DOM event.
// May only be used within event handler in same goroutine.
StopPropagation()
}
// domEvent implements the DOMEvent interface.
// This way DOMEvent can be passed around without a pointer this helps make
// DOM events and component events similar and consistent.
// It might actually makes sense to move this into the domrender package at some point.
type domEvent struct {
eventSummary map[string]interface{}
eventEnv EventEnv // from the renderer
// TODO: yeah but why? this does not need to be here
window js.Value // sure, why not
}
var _ DOMEvent = &domEvent{} // assert domEvent implements DOMEvent
// Prop returns a value from the EventSummary using the keys you specify.
// The keys is a list of map keys to be looked up. For example:
// e.Prop("target", "name") will return the same value as e.EventSummary()["target"]["name"],
// except that Prop helps with some edge cases and if a value is missing
// of the wrong type, nil will be returned, instead of panicing.
func (e *domEvent) Prop(keys ...string) interface{} {
var ret interface{}
ret = e.eventSummary
for _, key := range keys {
// see if ret is a map
m, _ := ret.(map[string]interface{})
if m == nil {
return nil
}
// and index into the map if so, replacing ret
ret = m[key]
}
return ret
}
// PropString is like Prop but returns it's value as a string.
// No type conversion is done, if the requested value is not
// already a string then an empty string will be returned.
func (e *domEvent) PropString(keys ...string) string {
ret, _ := e.Prop(keys...).(string)
return ret
}
// PropFloat64 is like Prop but returns it's value as a float64.
// No type conversion is done, if the requested value is not
// already a float64 then float64(0) will be returned.
func (e *domEvent) PropFloat64(keys ...string) float64 {
ret, _ := e.Prop(keys...).(float64)
return ret
}
// PropBool is like Prop but returns it's value as a bool.
// No type conversion is done, if the requested value is not
// already a bool then false will be returned.
func (e *domEvent) PropBool(keys ...string) bool {
ret, _ := e.Prop(keys...).(bool)
return ret
}
// EventSummary returns a map with simple properties (primitive types) from the event.
// Accessing values returns by EventSummary incurs no additional performance or memory
// penalty, whereas calls to JSEvent, JSEventTarget, etc. require a call into the browser
// JS engine and the attendant resource usage. So if you can get the information you
// need from the EventSummary, that's better.
func (e *domEvent) EventSummary() map[string]interface{} {
return e.eventSummary
}
// JSEvent this returns a js.Value in wasm that corresponds to the event object.
// Non-wasm implementation returns nil.
func (e *domEvent) JSEvent() js.Value {
return e.window.Call("vuguGetActiveEvent")
}
// JSEventTarget returns the value of the "target" property of the event, the element
// that the event was originally fired/registered on.
func (e *domEvent) JSEventTarget() js.Value {
return e.window.Call("vuguGetActiveEventTarget")
}
// JSEventCurrentTarget returns the value of the "currentTarget" property of the event, the element
// that is currently processing the event.
func (e *domEvent) JSEventCurrentTarget() js.Value {
return e.window.Call("vuguGetActiveEventCurrentTarget")
}
// EventEnv returns the EventEnv for the current environment and allows locking and unlocking around modifications.
// See EventEnv struct.
func (e *domEvent) EventEnv() EventEnv {
return e.eventEnv
}
// PreventDefault calls preventDefault() on the underlying DOM event.
// May only be used within event handler in same goroutine.
func (e *domEvent) PreventDefault() {
e.window.Call("vuguActiveEventPreventDefault")
}
// StopPropagation calls stopPropagation() on the underlying DOM event.
// May only be used within event handler in same goroutine.
func (e *domEvent) StopPropagation() {
e.window.Call("vuguActiveEventStopPropagation")
}
// DOMEventHandlerSpec describes an event that gets registered with addEventListener.
type DOMEventHandlerSpec struct {
EventType string // "click", "mouseover", etc.
Func func(DOMEvent)
Capture bool
Passive bool
}
// // DOMEventHandler is created in BuildVDOM to represent a method call that is performed to handle an event.
// type DOMEventHandler struct {
// ReceiverAndMethodHash uint64 // hash value corresponding to the method and receiver, so we get a unique value for each combination of method and receiver
// Method reflect.Value // method to be called, with receiver baked into it if needed (see reflect/Value.MethodByName)
// Args []interface{} // arguments to be passed in when calling (special case for eventStub)
// }
// func (d DOMEventHandler) hashString() string {
// return fmt.Sprintf("%x", d.hash())
// }
// func (d DOMEventHandler) hash() (ret uint64) {
// // defer func() {
// // log.Printf("DOMEventHandler.hash for (receiver_and_method_hash=%v, method=%#v, args=%#v) is returning %v", d.ReceiverAndMethodHash, d.Method, d.Args, ret)
// // }()
// if !d.Method.IsValid() && len(d.Args) == 0 {
// return 0
// }
// b8 := make([]byte, 8)
// h := xxhash.New()
// // this may end up being an issue but I need to move through this for now -
// // this is only unique for the method itself, not the instance it's tied to, although
// // it's probably rare to have to distinguish between multiple component types in use in an application
// // fmt.Fprintf(h, "%#v", d.Method)
// // NOTE: added ReceiverAndMethodHash to deal with the note above, let's see if it solves the problem
// binary.BigEndian.PutUint64(b8, d.ReceiverAndMethodHash)
// h.Write(b8)
// // add the args
// for _, a := range d.Args {
// binary.BigEndian.PutUint64(b8, ComputeHash(a))
// h.Write(b8)
// }
// return h.Sum64()
// }
// EventEnv provides locking mechanism to for rendering environment to events so
// data access and rendering can be synchronized and avoid race conditions.
type EventEnv interface {
Lock() // acquire write lock
UnlockOnly() // release write lock
UnlockRender() // release write lock and request re-render
RLock() // acquire read lock
RUnlock() // release read lock
}
// NewEventEnvImpl creates and returns a new EventEnvImpl.
func NewEventEnvImpl(rwmu *sync.RWMutex, requestRenderCH chan bool) *EventEnvImpl {
return &EventEnvImpl{
rwmu: rwmu,
requestRenderCH: requestRenderCH,
}
}
// EventEnvImpl implements EventEnv
type EventEnvImpl struct {
rwmu *sync.RWMutex
requestRenderCH chan bool
}
// Lock will acquire write lock
func (ee *EventEnvImpl) Lock() {
// if ee.rwmu == nil {
// return
// }
ee.rwmu.Lock()
}
// UnlockOnly will release the write lock
func (ee *EventEnvImpl) UnlockOnly() {
// if ee.rwmu == nil {
// return
// }
ee.rwmu.Unlock()
}
// UnlockRender will release write lock and request re-render
func (ee *EventEnvImpl) UnlockRender() {
// if ee.rwmu != nil {
ee.rwmu.Unlock()
// }
if ee.requestRenderCH != nil {
// send non-blocking
select {
case ee.requestRenderCH <- true:
default:
}
}
}
// RLock will acquire a read lock
func (ee *EventEnvImpl) RLock() {
// if ee.rwmu == nil {
// return
// }
ee.rwmu.RLock()
}
// RUnlock will release the read lock
func (ee *EventEnvImpl) RUnlock() {
// if ee.rwmu == nil {
// return
// }
ee.rwmu.RUnlock()
}