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

View file

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

View file

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

View file

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