Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Oct 23, 2023
1 parent 094890d commit ce2a465
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 225 deletions.
3 changes: 1 addition & 2 deletions jaws.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,8 @@ func (jw *Jaws) ServeWithTimeout(requestTimeout time.Duration) {
// could mean nonreproducible and seemingly
// random failures in processing logic.
mustBroadcast := func(msg Message) {
isCmd := msg.What.IsCommand()
for msgCh, rq := range subs {
if isCmd || rq.wantMessage(&msg) {
if msg.Dest == nil || rq.wantMessage(&msg) {
select {
case msgCh <- msg:
default:
Expand Down
51 changes: 17 additions & 34 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,21 +302,15 @@ func (rq *Request) Dirty(tags ...interface{}) {

// wantMessage returns true if the Request want the message.
func (rq *Request) wantMessage(msg *Message) (yes bool) {
if rq != nil {
switch dest := msg.Dest.(type) {
case *Request:
yes = dest == rq
case string: // HTML id
yes = true
case *Element:
yes = dest.Request == rq
case jid.Jid:
yes = rq.GetElement(dest) != nil
default:
rq.mu.RLock()
_, yes = rq.tagMap[msg.Dest]
rq.mu.RUnlock()
}
switch dest := msg.Dest.(type) {
case *Request:
yes = dest == rq
case string: // HTML id
yes = true
default:
rq.mu.RLock()
_, yes = rq.tagMap[msg.Dest]
rq.mu.RUnlock()
}
return
}
Expand All @@ -339,15 +333,14 @@ func (rq *Request) NewElement(ui UI) *Element {
return rq.newElementLocked(ui)
}

func (rq *Request) getElementLocked(jid Jid) *Element {
if jid > 0 {
for _, elem := range rq.elems {
if elem.jid == jid {
return elem
}
func (rq *Request) getElementLocked(jid Jid) (elem *Element) {
for _, e := range rq.elems {
if e.jid == jid {
elem = e
break
}
}
return nil
return
}

func (rq *Request) GetElement(jid Jid) (e *Element) {
Expand Down Expand Up @@ -511,29 +504,19 @@ func (rq *Request) process(broadcastMsgCh chan Message, incomingMsgCh <-chan wsM
// prepare the data to send in the WS message
var wsdata string
switch data := tagmsg.Data.(type) {
case nil:
// do nothing
case string:
wsdata = data
case template.HTML:
wsdata = string(data)
case []interface{}: // list of tags
wsdata = rq.makeIdList(data)
default:
// do nothing
}

// collect all elements marked with the tag in the message
var todo []*Element
switch v := tagmsg.Dest.(type) {
case nil:
// matches no elements
case *Element:
if v.Request == rq {
todo = append(todo, v)
}
case Jid:
if elem := rq.GetElement(v); elem != nil {
todo = append(todo, elem)
}
case string:
// target is a regular HTML ID
wsQueue = append(wsQueue, wsMsg{
Expand Down
Loading

0 comments on commit ce2a465

Please sign in to comment.