work in progress: action handling
This commit is contained in:
parent
5e60c3e10b
commit
5c6a7c76d8
4 changed files with 51 additions and 5 deletions
|
@ -18,3 +18,43 @@ func (spec *baseSpec) Receivers() []string {
|
||||||
func BaseSpec(hdlr lib.ActionHandler, rcvrs []string) *baseSpec {
|
func BaseSpec(hdlr lib.ActionHandler, rcvrs []string) *baseSpec {
|
||||||
return &baseSpec{hdlr, rcvrs}
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.sr.ht/~cco/go-scopes/lib"
|
"git.sr.ht/~cco/go-scopes/lib"
|
||||||
|
"git.sr.ht/~cco/go-scopes/lib/action"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(ctx lib.Context) {
|
func Start(ctx lib.Context) {
|
||||||
|
@ -29,7 +30,12 @@ func Step(ctx lib.Context) (loop bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleMessage(ctx lib.Context, msg lib.Message) bool {
|
func HandleMessage(ctx lib.Context, msg lib.Message) (loop bool) {
|
||||||
fmt.Println("HandleMessage", msg)
|
loop = true
|
||||||
return true
|
fmt.Println("core.HandleMessage", msg)
|
||||||
|
for _, act := range action.Select(ctx, msg) {
|
||||||
|
//loop = act.Spec().Handler()(act)
|
||||||
|
loop = act.Handle()
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,7 @@ type Action interface {
|
||||||
Context() Context
|
Context() Context
|
||||||
Spec() ActionSpec
|
Spec() ActionSpec
|
||||||
Message() Message
|
Message() Message
|
||||||
|
Handle() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Proc = func(Context)
|
type Proc = func(Context)
|
||||||
|
|
|
@ -27,8 +27,7 @@ func Config() lib.Config {
|
||||||
return &cfg
|
return &cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// register action handlers from ..._test.go here:
|
// register action handlers from ..._test.go here.
|
||||||
|
|
||||||
var (
|
var (
|
||||||
AH_Receiver lib.ActionHandler
|
AH_Receiver lib.ActionHandler
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue