Finished the widgetizing.

This commit is contained in:
Andrey Parhomenko 2023-09-16 15:40:30 +03:00
parent 57f85fdacc
commit 86cfffc56f
4 changed files with 19 additions and 20 deletions

View file

@ -166,7 +166,7 @@ func (widget *CommandWidget) Serve(
commanders, commanders,
) )
var cmdUpdates chan *Update var cmdUpdates *UpdateChan
for u := range updates.Chan() { for u := range updates.Chan() {
if c.ScreenId() == "" && u.Message != nil { if c.ScreenId() == "" && u.Message != nil {
// Skipping and executing the preinit action // Skipping and executing the preinit action
@ -189,15 +189,14 @@ func (widget *CommandWidget) Serve(
c.Run(cmd.Action, u) c.Run(cmd.Action, u)
if cmd.Widget != nil { if cmd.Widget != nil {
if cmdUpdates != nil { cmdUpdates.Close()
close(cmdUpdates) cmdUpdates = NewUpdateChan()
}
cmdUpdates := NewUpdateChan()
go func() { go func() {
cmd.Widget.Serve( cmd.Widget.Serve(
&Context{context: c.context, Update: u}, &Context{context: c.context, Update: u},
cmdUpdates, cmdUpdates,
) )
cmdUpdates.Close()
cmdUpdates = nil cmdUpdates = nil
}() }()
} }
@ -207,7 +206,7 @@ func (widget *CommandWidget) Serve(
if cmdUpdates != nil { if cmdUpdates != nil {
// Send to the commands channel if we are // Send to the commands channel if we are
// executing one. // executing one.
cmdUpdates <- u cmdUpdates.Send(u)
} else { } else {
c.Skip(u) c.Skip(u)
} }

View file

@ -112,7 +112,7 @@ func (p *Page) Serve(
replyUpdates.Send(u ) replyUpdates.Send(u )
case p.SubWidget != nil : case p.SubWidget != nil :
if subFilterOk { if subFilterOk {
if subFilter.Filter(u, msgs) { if !subFilter.Filter(u, msgs) {
subUpdates.Send(u) subUpdates.Send(u)
} }
} else { } else {

View file

@ -119,19 +119,20 @@ func (c *Context) ChangeScreen(screenId ScreenId, args ...any) error {
c.prevScreenId = c.screenId c.prevScreenId = c.screenId
c.screenId = screenId c.screenId = screenId
// Making the new channel for the widget. // Stopping the current widget.
c.skippedUpdates.Close() c.skippedUpdates.Close()
// Making channel for the new widget.
c.skippedUpdates = NewUpdateChan() c.skippedUpdates = NewUpdateChan()
if screen.Widget != nil { if screen.Widget != nil {
// Running the widget if the screen has one. // Running the widget if the screen has one.
go func() { go func() {
updates := c.skippedUpdates
screen.Widget.Serve(&Context{ screen.Widget.Serve(&Context{
context: c.context, context: c.context,
Update: c.Update, Update: c.Update,
Args: args, Args: args,
}, c.skippedUpdates) }, updates)
updates.Close()
c.skippedUpdates.Close()
}() }()
} else { } else {
panic("no widget defined for the screen") panic("no widget defined for the screen")

View file

@ -2,6 +2,7 @@ package tg
import ( import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
//"fmt"
) )
// Implementing the interface provides // Implementing the interface provides
@ -183,19 +184,17 @@ func (widget *InlineKeyboardWidget) Filter(
u *Update, u *Update,
msgs MessageMap, msgs MessageMap,
) bool { ) bool {
if widget == nil { if widget == nil || u.CallbackQuery == nil {
return true
}
if u.CallbackQuery == nil || len(msgs) < 1 {
return true return true
} }
inlineMsg, inlineOk := msgs[""] inlineMsg, inlineOk := msgs[""]
if inlineOk { if !inlineOk {
if u.CallbackQuery.Message.MessageID != return true
inlineMsg.MessageID { }
return true if u.CallbackQuery.Message.MessageID !=
} inlineMsg.MessageID {
return true
} }
return false return false