-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventhandler.go
45 lines (35 loc) · 1.21 KB
/
eventhandler.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
package jaws
import (
"github.com/linkdata/jaws/what"
)
type EventHandler interface {
JawsEvent(e *Element, wht what.What, val string) (err error)
}
type errEventUnhandled struct{}
func (errEventUnhandled) Error() string {
return "event unhandled"
}
// ErrEventUnhandled returned by JawsEvent() or JawsClick() causes the next
// available handler to be invoked.
var ErrEventUnhandled = errEventUnhandled{}
// EventFn is the signature of a event handling function to be called when JaWS receives
// an event message from the Javascript via the WebSocket connection.
type EventFn = func(e *Element, wht what.What, val string) (err error)
type eventFnWrapper struct{ EventFn }
func (ehf eventFnWrapper) JawsEvent(e *Element, w what.What, v string) (err error) {
return ehf.EventFn(e, w, v)
}
var _ EventFn = eventFnWrapper{}.JawsEvent // statically ensure JawsEvent and EventFn are compatible
func callEventHandler(obj any, e *Element, wht what.What, val string) (err error) {
if wht == what.Click {
if h, ok := obj.(ClickHandler); ok {
if err = h.JawsClick(e, val); err != ErrEventUnhandled {
return
}
}
}
if h, ok := obj.(EventHandler); ok {
return h.JawsEvent(e, wht, val)
}
return ErrEventUnhandled
}