From c84949537ef749a0f56158160378038553f418ad Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Fri, 10 Sep 2021 21:32:51 +0300 Subject: [PATCH] Add channel.ignoreReads() --- channel.go | 15 +++++++++++++++ client.go | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/channel.go b/channel.go index 6f07345..46111a4 100644 --- a/channel.go +++ b/channel.go @@ -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 @@ -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) @@ -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 } diff --git a/client.go b/client.go index d0cc717..858be5e 100644 --- a/client.go +++ b/client.go @@ -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: