Fixed active value quering since it makes CP heat as...

This commit is contained in:
Andrey Parhomenko 2023-08-11 12:45:50 +03:00
parent ee28fcf2f7
commit d3922a14e1
2 changed files with 15 additions and 21 deletions

View file

@ -10,9 +10,10 @@ import (
// handling functions. Is provided to Act() function always. // handling functions. Is provided to Act() function always.
type Context struct { type Context struct {
*Session *Session
B *Bot B *Bot
updates chan *Update updates chan *Update
available bool available bool
availableChan chan bool
} }
// Goroutie function to handle each user. // Goroutie function to handle each user.
@ -39,7 +40,7 @@ func (c *Context) handleUpdateChan(updates chan *Update) {
// Sending wrong messages to // Sending wrong messages to
// the currently reading goroutine. // the currently reading goroutine.
if !ok && c.ReadingUpdate { if !ok && c.readingUpdate {
c.updates <- u c.updates <- u
continue continue
} }
@ -64,7 +65,6 @@ func (c *Context) handleUpdateChan(updates chan *Update) {
} }
func (c *Context) run(a Action) { func (c *Context) run(a Action) {
c.available = true
go a.Act(c) go a.Act(c)
} }
@ -89,7 +89,9 @@ func (c *Context) ChangeScreen(screenId ScreenId) error {
c.Session.ChangeScreen(screenId) c.Session.ChangeScreen(screenId)
c.KeyboardId = screen.KeyboardId c.KeyboardId = screen.KeyboardId
c.available = false if c.readingUpdate {
c.updates <- nil
}
if screen.Action != nil { if screen.Action != nil {
c.run(screen.Action) c.run(screen.Action)
} }
@ -99,22 +101,14 @@ func (c *Context) ChangeScreen(screenId ScreenId) error {
// Returns the next update ignoring current screen. // Returns the next update ignoring current screen.
func (c *Context) ReadUpdate() (*Update, error) { func (c *Context) ReadUpdate() (*Update, error) {
var ( c.readingUpdate = true
u *Update u := <-c.updates
) c.readingUpdate = false
c.ReadingUpdate = true if u == nil {
for { return nil, NotAvailableErr
select {
case u = <-c.updates:
c.ReadingUpdate = false
return u, nil
default:
if !c.available {
return nil, NotAvailableErr
}
}
} }
return u, nil
} }
// Returns the next text message that the user sends. // Returns the next text message that the user sends.

View file

@ -23,7 +23,7 @@ type Session struct {
KeyboardId KeyboardId KeyboardId KeyboardId
// Is true if currently reading the Update. // Is true if currently reading the Update.
ReadingUpdate bool readingUpdate bool
// Custom data for each user. // Custom data for each user.
V map[string]any V map[string]any