From 6c1b889c2f7550005a612281e9836ee8c60887e1 Mon Sep 17 00:00:00 2001 From: Yilun Date: Wed, 24 Jan 2024 01:53:03 -0800 Subject: [PATCH] Remove bool return in MaybeNext for gomobile compatibility Signed-off-by: Yilun --- util.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/util.go b/util.go index 1e40ff1f..9d1022b1 100644 --- a/util.go +++ b/util.go @@ -119,14 +119,14 @@ func (c *OnConnect) Next() *Node { return <-c.C } -// MaybeNext returns the next element in the channel and true, if channel is -// empty it returns nil and false. -func (c *OnConnect) MaybeNext() (*Node, bool) { +// MaybeNext returns the next element in the channel, or nil if channel is +// empty. +func (c *OnConnect) MaybeNext() *Node { select { case v := <-c.C: - return v, true + return v default: - return nil, false + return nil } } @@ -168,14 +168,14 @@ func (c *OnMessage) Next() *Message { return <-c.C } -// MaybeNext returns the next element in the channel and true, if channel is -// empty it returns nil and false. -func (c *OnMessage) MaybeNext() (*Message, bool) { +// MaybeNext returns the next element in the channel, or nil if channel is +// empty. +func (c *OnMessage) MaybeNext() *Message { select { case v := <-c.C: - return v, true + return v default: - return nil, false + return nil } } @@ -233,6 +233,17 @@ func (c *OnError) Next() error { return <-c.C } +// MaybeNext returns the next element in the channel, or nil if channel is +// empty. +func (c *OnError) MaybeNext() error { + select { + case v := <-c.C: + return v + default: + return nil + } +} + func (c *OnError) receive(err error) { if c.Callback != nil { c.Callback.OnError(err)