Skip to content

Commit

Permalink
Add channel.ignoreReads()
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Sep 10, 2021
1 parent 859d6f5 commit c849495
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Channel struct {
logger *logrus.Entry
confCh chan bool
readCh chan *cableMsg

ignoreReads bool
}

// Perform sends passed action with additional data to the channel
Expand All @@ -37,6 +39,11 @@ func (ch *Channel) Perform(action string, attr goja.Value) error {
})
}

// IgnoreReads allows skipping collecting incoming messages (in case you only care about the subscription)
func (ch *Channel) IgnoreReads() {
ch.ignoreReads = true
}

// Receive checks channels messages query for message, sugar for ReceiveN(1, attrs)
func (ch *Channel) Receive(attr goja.Value) interface{} {
results := ch.ReceiveN(1, attr)
Expand Down Expand Up @@ -78,6 +85,14 @@ func (ch *Channel) ReceiveN(n int, cond goja.Value) []interface{} {
}
}

func (ch *Channel) handleIncoming(msg *cableMsg) {
if ch.ignoreReads {
return
}

ch.readCh <- msg
}

type Matcher interface {
Match(msg interface{}) bool
}
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *Client) handleLoop() {
case "reject_subscription":
c.channels[msg.Identifier].confCh <- false
default:
c.channels[msg.Identifier].readCh <- msg
c.channels[msg.Identifier].handleIncoming(msg)
}
}
case err := <-c.errorCh:
Expand Down

0 comments on commit c849495

Please sign in to comment.