From d3922a14e16b773ca11f7b2069fed3cc51198343 Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 11 Aug 2023 12:45:50 +0300 Subject: [PATCH] Fixed active value quering since it makes CP heat as... --- src/tx/context.go | 34 ++++++++++++++-------------------- src/tx/session.go | 2 +- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/tx/context.go b/src/tx/context.go index cbe889d..b1c0b91 100644 --- a/src/tx/context.go +++ b/src/tx/context.go @@ -10,9 +10,10 @@ import ( // handling functions. Is provided to Act() function always. type Context struct { *Session - B *Bot - updates chan *Update - available bool + B *Bot + updates chan *Update + available bool + availableChan chan bool } // Goroutie function to handle each user. @@ -39,7 +40,7 @@ func (c *Context) handleUpdateChan(updates chan *Update) { // Sending wrong messages to // the currently reading goroutine. - if !ok && c.ReadingUpdate { + if !ok && c.readingUpdate { c.updates <- u continue } @@ -64,7 +65,6 @@ func (c *Context) handleUpdateChan(updates chan *Update) { } func (c *Context) run(a Action) { - c.available = true go a.Act(c) } @@ -89,7 +89,9 @@ func (c *Context) ChangeScreen(screenId ScreenId) error { c.Session.ChangeScreen(screenId) c.KeyboardId = screen.KeyboardId - c.available = false + if c.readingUpdate { + c.updates <- nil + } if screen.Action != nil { c.run(screen.Action) } @@ -99,22 +101,14 @@ func (c *Context) ChangeScreen(screenId ScreenId) error { // Returns the next update ignoring current screen. func (c *Context) ReadUpdate() (*Update, error) { - var ( - u *Update - ) - c.ReadingUpdate = true - for { - select { - case u = <-c.updates: - c.ReadingUpdate = false - return u, nil - default: - if !c.available { - return nil, NotAvailableErr - } - } + c.readingUpdate = true + u := <-c.updates + c.readingUpdate = false + if u == nil { + return nil, NotAvailableErr } + return u, nil } // Returns the next text message that the user sends. diff --git a/src/tx/session.go b/src/tx/session.go index 085cf88..ee83bfa 100644 --- a/src/tx/session.go +++ b/src/tx/session.go @@ -23,7 +23,7 @@ type Session struct { KeyboardId KeyboardId // Is true if currently reading the Update. - ReadingUpdate bool + readingUpdate bool // Custom data for each user. V map[string]any