-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
257 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package jaws | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/linkdata/jaws/what" | ||
) | ||
|
||
type JsAny struct { | ||
JsVariable | ||
AnySetter | ||
} | ||
|
||
func (ui *JsAny) JawsRender(e *Element, w io.Writer, params []any) error { | ||
return ui.render(ui.AnySetter, ui.JawsGetAny(e), e, w, params) | ||
} | ||
|
||
func (ui *JsAny) JawsUpdate(e *Element) { | ||
_ = e.JsSet(ui.JawsGetAny(e)) | ||
} | ||
|
||
func (ui *JsAny) JawsEvent(e *Element, wht what.What, val string) (err error) { | ||
err = ErrEventUnhandled | ||
if wht == what.Set { | ||
var v any | ||
if err = json.Unmarshal([]byte(val), &v); err == nil { | ||
_, err = e.maybeDirty(ui.Tag, ui.JawsSetAny(e, v)) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func NewJsAny(g AnySetter, name string) *JsAny { | ||
return &JsAny{ | ||
JsVariable: JsVariable{JsName{Name: name}}, | ||
AnySetter: g, | ||
} | ||
} | ||
|
||
func (rq RequestWriter) JsAny(value any, name string, params ...any) error { | ||
return rq.UI(NewJsAny(makeAnySetter(value), name), params...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package jaws | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/linkdata/jaws/what" | ||
) | ||
|
||
func TestJsAny_JawsRender(t *testing.T) { | ||
th := newTestHelper(t) | ||
nextJid = 0 | ||
rq := newTestRequest() | ||
defer rq.Close() | ||
|
||
var val any | ||
ts := newTestSetter(val) | ||
th.NoErr(rq.JsAny(ts, "varname")) | ||
wantHtml := "<div id=\"Jid.1\" data-jawsname=\"varname\" hidden></div>" | ||
if gotHtml := rq.BodyString(); gotHtml != wantHtml { | ||
t.Errorf("Request.JsAny() = %q, want %q", gotHtml, wantHtml) | ||
} | ||
|
||
ts.Set(1.3) | ||
rq.Dirty(ts) | ||
|
||
select { | ||
case <-th.C: | ||
th.Timeout() | ||
case msg := <-rq.outCh: | ||
s := msg.Format() | ||
if s != "Set\tJid.1\t1.3\n" { | ||
t.Error(strconv.Quote(s)) | ||
} | ||
} | ||
} | ||
|
||
func TestJsAny_JawsEvent(t *testing.T) { | ||
th := newTestHelper(t) | ||
nextJid = 0 | ||
rq := newTestRequest() | ||
defer rq.Close() | ||
|
||
msgCh := make(chan string, 1) | ||
defer close(msgCh) | ||
|
||
val := float64(1.2) | ||
ts := newTestSetter(any(val)) | ||
ui := NewJsAny(ts, "varname") | ||
th.NoErr(rq.UI(ui)) | ||
|
||
th.Equal(ui.JawsGetTag(rq.Request), ts) | ||
|
||
select { | ||
case <-th.C: | ||
th.Timeout() | ||
case rq.inCh <- wsMsg{Jid: 1, What: what.Set, Data: "1.3"}: | ||
} | ||
|
||
select { | ||
case <-th.C: | ||
th.Timeout() | ||
case <-ts.setCalled: | ||
} | ||
|
||
th.Equal(ts.Get(), 1.3) | ||
|
||
select { | ||
case <-th.C: | ||
th.Timeout() | ||
case rq.inCh <- wsMsg{Jid: 1, What: what.Set, Data: "1.3"}: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,50 @@ | ||
package jaws | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/linkdata/jaws/what" | ||
) | ||
|
||
type JsFunction struct { | ||
JsName | ||
Param AnySetter | ||
Result AnySetter | ||
ResultTag any | ||
} | ||
|
||
func (ui *JsFunction) JawsRender(e *Element, w io.Writer, params []any) error { | ||
ui.ResultTag = e.ApplyGetter(ui.Result) | ||
return ui.render(ui.Param, nil, e, w, params) | ||
} | ||
|
||
func (ui *JsFunction) JawsUpdate(e *Element) { | ||
_ = e.JsCall(ui.Param.JawsGetAny(e)) | ||
} | ||
|
||
func (ui *JsFunction) JawsEvent(e *Element, wht what.What, val string) (err error) { | ||
err = ErrEventUnhandled | ||
if wht == what.Set && ui.ResultTag != nil { | ||
var v any | ||
if err = json.Unmarshal([]byte(val), &v); err == nil { | ||
_, err = e.maybeDirty(ui.ResultTag, ui.Result.JawsSetAny(e, v)) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func NewJsFunction(param, result AnySetter, name string) *JsFunction { | ||
if param == nil { | ||
panic("NewJsFunction param is nil") | ||
} | ||
return &JsFunction{ | ||
JsName: JsName{Name: name}, | ||
Param: param, | ||
Result: result, | ||
} | ||
} | ||
|
||
func (rq RequestWriter) JsFunction(param, result any, name string, params ...any) error { | ||
return rq.UI(NewJsFunction(makeAnySetter(param), makeAnySetter(result), name), params...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package jaws | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/linkdata/jaws/what" | ||
) | ||
|
||
func TestJsFunction_JawsRender(t *testing.T) { | ||
th := newTestHelper(t) | ||
nextJid = 0 | ||
rq := newTestRequest() | ||
defer rq.Close() | ||
|
||
param := any("meh") | ||
tsparam := newTestSetter(param) | ||
result := any("foo") | ||
tsresult := newTestSetter(result) | ||
ui := NewJsFunction(tsparam, tsresult, "fnname") | ||
th.NoErr(rq.UI(ui)) | ||
wantHtml := "<div id=\"Jid.1\" data-jawsname=\"fnname\" hidden></div>" | ||
if gotHtml := rq.BodyString(); gotHtml != wantHtml { | ||
t.Errorf("Request.JsFunction() = %q, want %q", gotHtml, wantHtml) | ||
} | ||
|
||
tsparam.Set(1.3) | ||
rq.Dirty(tsparam) | ||
|
||
select { | ||
case <-th.C: | ||
th.Timeout() | ||
case msg := <-rq.outCh: | ||
s := msg.Format() | ||
if s != "Call\tJid.1\t1.3\n" { | ||
t.Error(strconv.Quote(s)) | ||
} | ||
} | ||
} | ||
|
||
func TestJsFunction_PanicsIfParamNil(t *testing.T) { | ||
is := newTestHelper(t) | ||
rq := newTestRequest() | ||
defer rq.Close() | ||
defer func() { | ||
if x := recover(); x == nil { | ||
is.Fail() | ||
} | ||
}() | ||
NewJsFunction(nil, nil, "fnname") | ||
is.Fail() | ||
} | ||
|
||
func TestJsFunction_JawsEvent(t *testing.T) { | ||
th := newTestHelper(t) | ||
nextJid = 0 | ||
rq := newTestRequest() | ||
defer rq.Close() | ||
|
||
param := any("foo") | ||
tsparam := newTestSetter(param) | ||
result := any(float64(1.2)) | ||
tsresult := newTestSetter(result) | ||
th.NoErr(rq.JsFunction(tsparam, tsresult, "fnname")) | ||
|
||
select { | ||
case <-th.C: | ||
th.Timeout() | ||
case rq.inCh <- wsMsg{Jid: 1, What: what.Set, Data: "1.3"}: | ||
} | ||
|
||
select { | ||
case <-th.C: | ||
th.Timeout() | ||
case <-tsresult.setCalled: | ||
} | ||
|
||
th.Equal(tsresult.Get(), 1.3) | ||
|
||
select { | ||
case <-th.C: | ||
th.Timeout() | ||
case rq.inCh <- wsMsg{Jid: 1, What: what.Set, Data: "1.3"}: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters