Skip to content

Commit

Permalink
Merge pull request #29 from linkdata/eventfn-use-what-type
Browse files Browse the repository at this point in the history
change EventFn to use what.What
  • Loading branch information
linkdata authored Jul 4, 2023
2 parents 917c545 + f92947e commit 0c11bc7
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (rq *Request) OnClick(jid string, fn ClickFn) error {
// OnTrigger registers a jid and a function to be called when Trigger is called for it.
// Returns a nil error so it can be used inside templates.
func (rq *Request) OnTrigger(jid string, fn ClickFn) error {
rq.maybeEvent(jid, what.Trigger.String(), fn)
rq.maybeEvent(what.Trigger, jid, fn)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions namedboolarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ func (nba *NamedBoolArray) radioList(rq *Request, fn InputTextFn) (rl []Radio) {
return
}

func (nba *NamedBoolArray) radioEventFn(rq *Request, jid, evt, val string, fn InputTextFn) (err error) {
if evt == what.Input.String() && val != "" && strings.HasPrefix(jid, nba.prefix) {
func (nba *NamedBoolArray) radioEventFn(rq *Request, evt what.What, jid, val string, fn InputTextFn) (err error) {
if evt == what.Input && val != "" && strings.HasPrefix(jid, nba.prefix) {
var v bool
if v, err = strconv.ParseBool(val); err == nil {
name := strings.TrimPrefix(jid, nba.prefix)
Expand Down
10 changes: 7 additions & 3 deletions radio.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package jaws

import "html/template"
import (
"html/template"

"github.com/linkdata/jaws/what"
)

type Radio struct {
nba *NamedBoolArray
Expand All @@ -22,8 +26,8 @@ func (r *Radio) Radio(attrs ...string) template.HTML {
if r.Checked {
attrs = append(attrs, `checked`)
}
r.rq.RegisterEventFn(jid, func(rq *Request, id, evt, val string) error {
return r.nba.radioEventFn(rq, id, evt, val, r.fn)
r.rq.RegisterEventFn(jid, func(rq *Request, wht what.What, id, val string) error {
return r.nba.radioEventFn(rq, wht, id, val, r.fn)
})
return HtmlInput(jid, "radio", "", attrs...)
}
Expand Down
28 changes: 14 additions & 14 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ConnectFn func(rq *Request) error

// 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(rq *Request, id, evt, val string) error
type EventFn func(rq *Request, wht what.What, id, val string) error

// Request maintains the state for a JaWS WebSocket connection, and handles processing
// of events and broadcasts.
Expand Down Expand Up @@ -455,7 +455,7 @@ func (rq *Request) process(broadcastMsgCh chan *Message, incomingMsgCh <-chan *M
// an error to be sent out as an alert message.
// primary usecase is tests.
if msg.What == what.Hook {
msg = makeAlertDangerMessage(fn(rq, msg.Elem, msg.What.String(), msg.Data))
msg = makeAlertDangerMessage(fn(rq, msg.What, msg.Elem, msg.Data))
}

if msg != nil {
Expand All @@ -476,7 +476,7 @@ func (rq *Request) process(broadcastMsgCh chan *Message, incomingMsgCh <-chan *M
func (rq *Request) eventCaller(eventCallCh <-chan eventFnCall, outboundMsgCh chan<- *Message, eventDoneCh chan<- struct{}) {
defer close(eventDoneCh)
for call := range eventCallCh {
if err := call.fn(rq, call.msg.Elem, call.msg.What.String(), call.msg.Data); err != nil {
if err := call.fn(rq, call.msg.What, call.msg.Elem, call.msg.Data); err != nil {
select {
case outboundMsgCh <- makeAlertDangerMessage(err):
default:
Expand Down Expand Up @@ -508,10 +508,10 @@ func makeAlertDangerMessage(err error) (msg *Message) {
return
}

func (rq *Request) maybeEvent(id, event string, fn ClickFn) string {
func (rq *Request) maybeEvent(event what.What, id string, fn ClickFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
if evt == event {
err = fn(rq)
}
Expand All @@ -522,14 +522,14 @@ func (rq *Request) maybeEvent(id, event string, fn ClickFn) string {
}

func (rq *Request) maybeClick(jid string, fn ClickFn) string {
return rq.maybeEvent(jid, what.Click.String(), fn)
return rq.maybeEvent(what.Click, jid, fn)
}

func (rq *Request) maybeInputText(jid string, fn InputTextFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == what.Input.String() {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
if evt == what.Input {
err = fn(rq, val)
}
return
Expand All @@ -541,8 +541,8 @@ func (rq *Request) maybeInputText(jid string, fn InputTextFn) string {
func (rq *Request) maybeInputFloat(jid string, fn InputFloatFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == what.Input.String() {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
if evt == what.Input {
var v float64
if val != "" {
if v, err = strconv.ParseFloat(val, 64); err != nil {
Expand All @@ -560,8 +560,8 @@ func (rq *Request) maybeInputFloat(jid string, fn InputFloatFn) string {
func (rq *Request) maybeInputBool(jid string, fn InputBoolFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == what.Input.String() {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
if evt == what.Input {
var v bool
if val != "" {
if v, err = strconv.ParseBool(val); err != nil {
Expand All @@ -579,8 +579,8 @@ func (rq *Request) maybeInputBool(jid string, fn InputBoolFn) string {
func (rq *Request) maybeInputDate(jid string, fn InputDateFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == what.Input.String() {
wf = func(rq *Request, evt what.What, id, val string) (err error) {
if evt == what.Input {
var v time.Time
if val != "" {
if v, err = time.Parse(ISO8601, val); err != nil {
Expand Down
22 changes: 11 additions & 11 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestRequest_Registrations(t *testing.T) {
is.Equal(ok, true)
is.Equal(fn, nil)

var ef EventFn = func(rq *Request, id, evt, val string) error {
var ef EventFn = func(rq *Request, evt what.What, id, val string) error {
return nil
}
id2 := rq.RegisterEventFn(id, ef)
Expand Down Expand Up @@ -135,8 +135,8 @@ func TestRequest_DuplicateRegistration(t *testing.T) {
jw := New()
defer jw.Close()
rq := jw.NewRequest(context.Background(), nil)
var ef1 EventFn = func(rq *Request, id, evt, val string) error { return nil }
var ef2 EventFn = func(rq *Request, id, evt, val string) error { return errors.New("fails") }
var ef1 EventFn = func(rq *Request, evt what.What, id, val string) error { return nil }
var ef2 EventFn = func(rq *Request, evt what.What, id, val string) error { return errors.New("fails") }
is.Equal(rq.RegisterEventFn("foo", ef1), "foo") // first reg succeeds
is.Equal(rq.RegisterEventFn("foo", ef1), "foo") // second reg succeeds
rq2 := jw.UseRequest(rq.JawsKey, nil)
Expand Down Expand Up @@ -240,7 +240,7 @@ func TestRequest_OutboundRespectsJawsClosed(t *testing.T) {
defer rq.Close()
jw := rq.jw
var callCount int32
rq.RegisterEventFn("foo", func(rq *Request, id, evt, val string) error {
rq.RegisterEventFn("foo", func(rq *Request, evt what.What, id, val string) error {
atomic.AddInt32(&callCount, 1)
is.Equal(1, jw.RequestCount())
jw.Close()
Expand All @@ -262,7 +262,7 @@ func TestRequest_OutboundRespectsContextDone(t *testing.T) {
rq := newTestRequest(is)
defer rq.Close()
var callCount int32
rq.RegisterEventFn("foo", func(_ *Request, id, evt, val string) error {
rq.RegisterEventFn("foo", func(_ *Request, evt what.What, id, val string) error {
atomic.AddInt32(&callCount, 1)
rq.cancel()
return errors.New(val)
Expand Down Expand Up @@ -307,14 +307,14 @@ func TestRequest_Trigger(t *testing.T) {
defer rq.Close()
gotFooCall := make(chan struct{})
gotEndCall := make(chan struct{})
rq.RegisterEventFn("foo", func(rq *Request, id, evt, val string) error {
rq.RegisterEventFn("foo", func(rq *Request, evt what.What, id, val string) error {
defer close(gotFooCall)
return nil
})
rq.RegisterEventFn("err", func(rq *Request, id, evt, val string) error {
rq.RegisterEventFn("err", func(rq *Request, evt what.What, id, val string) error {
return errors.New(val)
})
rq.RegisterEventFn("end", func(rq *Request, id, evt, val string) error {
rq.RegisterEventFn("end", func(rq *Request, evt what.What, id, val string) error {
defer close(gotEndCall)
return nil
})
Expand Down Expand Up @@ -364,7 +364,7 @@ func TestRequest_EventFnQueue(t *testing.T) {
firstDoneCh := make(chan struct{})
var sleepDone int32
var callCount int32
rq.RegisterEventFn("sleep", func(rq *Request, id, evt, val string) error {
rq.RegisterEventFn("sleep", func(rq *Request, evt what.What, id, val string) error {
count := int(atomic.AddInt32(&callCount, 1))
is.Equal(val, strconv.Itoa(count))
if count == 1 {
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestRequest_EventFnQueueOverflowPanicsWithNoLogger(t *testing.T) {

var wait int32

rq.RegisterEventFn("bomb", func(_ *Request, id, evt, val string) error {
rq.RegisterEventFn("bomb", func(_ *Request, evt what.What, id, val string) error {
time.Sleep(time.Millisecond * time.Duration(atomic.AddInt32(&wait, 1)))
return nil
})
Expand Down Expand Up @@ -438,7 +438,7 @@ func TestRequest_IgnoresIncomingMsgsDuringShutdown(t *testing.T) {

var spewState int32
var callCount int32
rq.RegisterEventFn("spew", func(_ *Request, id, evt, val string) error {
rq.RegisterEventFn("spew", func(_ *Request, evt what.What, id, val string) error {
atomic.AddInt32(&callCount, 1)
if len(rq.outCh) < cap(rq.outCh) {
rq.jw.Trigger("spew", "")
Expand Down
2 changes: 1 addition & 1 deletion session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestSession_Delete(t *testing.T) {
rq2 := ts.jw.NewRequest(context.Background(), hr2)
is.Equal(ts.sess, rq2.Session())

ts.rq.RegisterEventFn("byebye", func(rq *Request, id, evt, val string) error {
ts.rq.RegisterEventFn("byebye", func(rq *Request, evt what.What, id, val string) error {
sess2 := ts.jw.GetSession(rq.Initial)
is.Equal(ts.sess, sess2)
cookie2 := sess2.Close()
Expand Down
2 changes: 1 addition & 1 deletion ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (tu testUi) JawsUi(rq *Request, attrs ...string) template.HTML {
return template.HTML(fmt.Sprintf(`<test jid="%s" %s>%s</test>`, rq.RegisterEventFn(tu.id, tu.JawsEvent), strings.Join(attrs, " "), tu.val))
}

func (tu testUi) JawsEvent(rq *Request, id, evt, val string) (err error) {
func (tu testUi) JawsEvent(rq *Request, evt what.What, id, val string) (err error) {
close(tu.gotCall)
return
}
Expand Down
2 changes: 1 addition & 1 deletion ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestWS_NormalExchange(t *testing.T) {

gotCallCh := make(chan struct{})

ts.rq.RegisterEventFn("foo", func(rq *Request, id, evt, val string) error {
ts.rq.RegisterEventFn("foo", func(rq *Request, evt what.What, id, val string) error {
close(gotCallCh)
return fooError
})
Expand Down

0 comments on commit 0c11bc7

Please sign in to comment.