minor refactorings

This commit is contained in:
Helmut Merz 2023-06-07 17:31:03 +02:00
parent 8b0230da3b
commit d37db07942
6 changed files with 27 additions and 24 deletions

View file

@ -75,11 +75,10 @@ func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base {
return cfg return cfg
} }
func (cfg *Base) WithAction(pattern string, spec lib.ActionSpec) lib.Config { func (cfg *Base) AddAction(pattern string, spec lib.ActionSpec) {
act := ActionConfig(pattern, nil) act := ActionConfig(pattern, nil)
act.specs = append(act.specs, spec) act.specs = append(act.specs, spec)
cfg.actions = append(cfg.actions, act) cfg.actions = append(cfg.actions, act)
return cfg
} }
func MakeBase(name string, starter lib.StartProc) *Base { func MakeBase(name string, starter lib.StartProc) *Base {

View file

@ -73,7 +73,7 @@ func Forward(act lib.Action) bool {
msg := act.Message() msg := act.Message()
for _, rcvr := range act.Spec().Receivers() { for _, rcvr := range act.Spec().Receivers() {
addr := message.SimpleAddress(rcvr) addr := message.SimpleAddress(rcvr)
message.Send(ctx, addr, msg) lib.Send(ctx, addr, msg)
} }
return true return true
} }

View file

@ -25,7 +25,7 @@ type Config interface {
Actions() []ActionConfig Actions() []ActionConfig
Children() []Config Children() []Config
Add(Config) Add(Config)
WithAction(string, ActionSpec) Config AddAction(string, ActionSpec)
} }
type Services map[string]Context type Services map[string]Context
@ -69,7 +69,7 @@ type StepProc = func(Context) bool
type MessageHandler = func(Context, Message) bool type MessageHandler = func(Context, Message) bool
type ActionHandler = func(Action) bool type ActionHandler = func(Action) bool
// async Runners // library functions
func RunCtx(ctx Context, fct Proc) { func RunCtx(ctx Context, fct Proc) {
ctx.WaitGroup().Add(1) ctx.WaitGroup().Add(1)
@ -78,3 +78,9 @@ func RunCtx(ctx Context, fct Proc) {
fct(ctx) fct(ctx)
}() }()
} }
func Send(ctx Context, addr Address, msg Message) {
if srv, ok := ctx.Services()[addr.Service()]; ok {
srv.Mailbox() <- msg
}
}

View file

@ -31,7 +31,7 @@ func (addr *address) Service() string {
} }
func (addr *address) Send(ctx lib.Context, msg lib.Message) { func (addr *address) Send(ctx lib.Context, msg lib.Message) {
Send(ctx, addr, msg) lib.Send(ctx, addr, msg)
} }
func SimpleAddress(srv string) *address { func SimpleAddress(srv string) *address {
@ -39,11 +39,3 @@ func SimpleAddress(srv string) *address {
service: srv, service: srv,
} }
} }
// public functions
func Send(ctx lib.Context, addr lib.Address, msg lib.Message) {
if srv, ok := ctx.Services()[addr.Service()]; ok {
srv.Mailbox() <- msg
}
}

View file

@ -12,20 +12,26 @@ import (
func Config() lib.Config { func Config() lib.Config {
ovr := Overrides().Use ovr := Overrides().Use
b := config.MakeBase b := config.MakeBase
cfg := app.Cfg{ cfg := app.Cfg{
Base: b("testing", testing.Start). Base: b("testing", testing.Start),
WithAction("demo",
action.BaseSpec(action.Forward,
[]string{"test-receiver"})).(*config.Base),
Home: ovr(".", HOME), Home: ovr(".", HOME),
AppType: "standard", AppType: "standard",
} }
cfg.Add(config.Cfg{ cfg.AddAction("demo",
action.BaseSpec(action.Forward,
[]string{"test-receiver"}))
cfg_config := config.Cfg{
Base: b("config", config.Start), Base: b("config", config.Start),
ConfigFormat: "etc", ConfigFormat: "etc",
}) }
cfg.Add(b("test-receiver", core.Start). cfg.Add(cfg_config)
WithAction("*", action.BaseSpec(AH_Receiver, nil)))
cfg_test_rcvr := b("test-receiver", core.Start)
cfg_test_rcvr.AddAction("*", action.BaseSpec(AH_Receiver, nil))
cfg.Add(cfg_test_rcvr)
return &cfg return &cfg
} }

View file

@ -36,9 +36,9 @@ func SendTest(t *testing.T) {
ctx := t.Ctx ctx := t.Ctx
rcvr := message.SimpleAddress("testing") rcvr := message.SimpleAddress("testing")
msg := message.StrMessage("demo") msg := message.StrMessage("demo")
rcvr.Send(ctx, msg) lib.Send(ctx, rcvr, msg)
rcvr = message.SimpleAddress("test-receiver") rcvr = message.SimpleAddress("test-receiver")
rcvr.Send(ctx, msg) lib.Send(ctx, rcvr, msg)
} }
// action handlers // action handlers