diff --git a/element_test.go b/element_test.go index 49f55c3..65a269b 100644 --- a/element_test.go +++ b/element_test.go @@ -79,8 +79,6 @@ func TestElement_Tag(t *testing.T) { is.True(!e.HasTag(Tag("zomg"))) e.Tag(Tag("zomg")) is.True(e.HasTag(Tag("zomg"))) - rq.mu.RLock() - defer rq.mu.RUnlock() s := e.String() if !strings.Contains(s, "zomg") { t.Error(s) diff --git a/request.go b/request.go index 6701f62..506711b 100644 --- a/request.go +++ b/request.go @@ -120,19 +120,28 @@ func (rq *Request) clearLocked() *Request { return rq } +func (rq *Request) renderDebug(elem *Element, w io.Writer) { + var sb strings.Builder + _, _ = fmt.Fprintf(&sb, "", "==>") + " -->")) +} + func (rq *Request) render(elem *Element, w io.Writer, params []any) (err error) { if err = elem.Ui().JawsRender(elem, w, params); err == nil { if rq.Jaws.Debug { - var sb strings.Builder - _, _ = fmt.Fprintf(&sb, "", "==>") + " -->")) + rq.renderDebug(elem, w) } } return @@ -265,23 +274,27 @@ func (rq *Request) Redirect(url string) { }) } -func (rq *Request) TagsOf(elem *Element) (tags []any) { - if elem != nil { - if rq.mu.TryRLock() { - defer rq.mu.RUnlock() - } - for tag, elems := range rq.tagMap { - for _, e := range elems { - if e == elem { - tags = append(tags, tag) - break - } +func (rq *Request) tagsOfLocked(elem *Element) (tags []any) { + for tag, elems := range rq.tagMap { + for _, e := range elems { + if e == elem { + tags = append(tags, tag) + break } } } return } +func (rq *Request) TagsOf(elem *Element) (tags []any) { + if elem != nil { + rq.mu.RLock() + defer rq.mu.RUnlock() + tags = rq.tagsOfLocked(elem) + } + return +} + // Register creates a new Element with the given tagitem as a valid target // for dynamic updates. // diff --git a/request_test.go b/request_test.go index 9074ba9..8709cf8 100644 --- a/request_test.go +++ b/request_test.go @@ -516,7 +516,6 @@ func TestRequest_DeleteByTag(t *testing.T) { t.Errorf("%q", s) } } - } func TestRequest_HtmlIdBroadcast(t *testing.T) { @@ -703,3 +702,31 @@ func TestRequest_CustomErrors(t *testing.T) { var target2 errPendingCancelled th.Equal(errors.As(cause, &target2), false) } + +func TestRequest_renderDebugLocked(t *testing.T) { + is := newTestHelper(t) + jw := New() + defer jw.Close() + rq := jw.NewRequest(nil) + defer jw.recycle(rq) + + tss := &testUi{} + e := rq.NewElement(tss) + e.Tag(Tag("zomg")) + + var sb strings.Builder + rq.renderDebug(e, &sb) + + txt := sb.String() + is.Equal(strings.Contains(txt, "zomg"), true) + is.Equal(strings.Contains(txt, "n/a"), false) + + rq.mu.Lock() + defer rq.mu.Unlock() + sb.Reset() + rq.renderDebug(e, &sb) + + txt = sb.String() + is.Equal(strings.Contains(txt, "zomg"), false) + is.Equal(strings.Contains(txt, "n/a"), true) +}