move HandleMsg() to lib

This commit is contained in:
Helmut Merz 2023-06-07 17:37:39 +02:00
parent d37db07942
commit 08263df110
3 changed files with 8 additions and 4 deletions

View file

@ -8,7 +8,6 @@ import (
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/context"
"git.sr.ht/~cco/go-scopes/lib/message"
)
@ -49,7 +48,7 @@ func step(ctx lib.Context, sig <-chan os.Signal) bool {
//ctx.LogInfo("Dispatcher stopped", m.Map{})
return false
}
return context.HandleMsg(ctx, msg)
return lib.HandleMsg(ctx, msg)
case <-ctx.Done():
return false
}

View file

@ -23,7 +23,7 @@ func Step(ctx lib.Context) (loop bool) {
//defer ctx.LogCatch("Step")
select {
case msg := <-ctx.Mailbox():
loop = ctx.HandleMsg(msg)
loop = lib.HandleMsg(ctx, msg)
case <-ctx.Done():
loop = false
}

View file

@ -4,6 +4,7 @@ package lib
import (
stdlib_context "context"
"fmt"
"sync"
)
@ -41,7 +42,6 @@ type Context interface {
State() ContextState
WithState(ContextState) Context
Mailbox() chan Message
HandleMsg(Message) bool
WaitGroup() *sync.WaitGroup
Stop()
}
@ -79,6 +79,11 @@ func RunCtx(ctx Context, fct Proc) {
}()
}
func HandleMsg(ctx Context, msg Message) bool {
fmt.Println("context.HandleMsg ", msg)
return ctx.Config().MessageHandler()(ctx, msg)
}
func Send(ctx Context, addr Address, msg Message) {
if srv, ok := ctx.Services()[addr.Service()]; ok {
srv.Mailbox() <- msg