work in progress: action handling

This commit is contained in:
Helmut Merz 2023-06-07 14:29:22 +02:00
parent 5e60c3e10b
commit 5c6a7c76d8
4 changed files with 51 additions and 5 deletions

View file

@ -18,3 +18,43 @@ func (spec *baseSpec) Receivers() []string {
func BaseSpec(hdlr lib.ActionHandler, rcvrs []string) *baseSpec {
return &baseSpec{hdlr, rcvrs}
}
// action selection
func Select(ctx lib.Context, msg lib.Message) []lib.Action {
var acts []lib.Action
// act := action{ctx, spec, msg}
// acts = append(acts, &act)
return acts
}
// action
type action struct {
ctx lib.Context
spec lib.ActionSpec
msg lib.Message
}
func (act *action) Context() lib.Context {
return act.ctx
}
func (act *action) Spec() lib.ActionSpec {
return act.spec
}
func (act *action) Message() lib.Message {
return act.msg
}
func (act *action) Handle() bool {
return act.spec.Handler()(act)
}
// predefined action handlers
func Forward(act lib.Action) bool {
// Send(ctx, addr. msg)
return true
}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/action"
)
func Start(ctx lib.Context) {
@ -29,7 +30,12 @@ func Step(ctx lib.Context) (loop bool) {
return
}
func HandleMessage(ctx lib.Context, msg lib.Message) bool {
fmt.Println("HandleMessage", msg)
return true
func HandleMessage(ctx lib.Context, msg lib.Message) (loop bool) {
loop = true
fmt.Println("core.HandleMessage", msg)
for _, act := range action.Select(ctx, msg) {
//loop = act.Spec().Handler()(act)
loop = act.Handle()
}
return
}

View file

@ -60,6 +60,7 @@ type Action interface {
Context() Context
Spec() ActionSpec
Message() Message
Handle() bool
}
type Proc = func(Context)

View file

@ -27,8 +27,7 @@ func Config() lib.Config {
return &cfg
}
// register action handlers from ..._test.go here:
// register action handlers from ..._test.go here.
var (
AH_Receiver lib.ActionHandler
)