Skip to content

Commit

Permalink
tags debug shows n/a for tags if not lockable
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Feb 21, 2024
1 parent dc72329 commit e9f1472
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
2 changes: 0 additions & 2 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 34 additions & 21 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<!-- id=%q %T tags=[", elem.Jid(), elem.Ui())
if rq.mu.TryRLock() {
defer rq.mu.RUnlock()
for i, tag := range rq.tagsOfLocked(elem) {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(TagString(tag))
}
} else {
sb.WriteString("n/a")
}
sb.WriteByte(']')
_, _ = w.Write([]byte(strings.ReplaceAll(sb.String(), "-->", "==>") + " -->"))
}

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, "<!-- id=%q %T tags=[", elem.Jid(), elem.Ui())
for i, tag := range elem.Request.TagsOf(elem) {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(TagString(tag))
}
sb.WriteByte(']')
_, _ = w.Write([]byte(strings.ReplaceAll(sb.String(), "-->", "==>") + " -->"))
rq.renderDebug(elem, w)
}
}
return
Expand Down Expand Up @@ -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.
//
Expand Down
29 changes: 28 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,6 @@ func TestRequest_DeleteByTag(t *testing.T) {
t.Errorf("%q", s)
}
}

}

func TestRequest_HtmlIdBroadcast(t *testing.T) {
Expand Down Expand Up @@ -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)
}

0 comments on commit e9f1472

Please sign in to comment.