minor refactorings
This commit is contained in:
parent
8b0230da3b
commit
d37db07942
6 changed files with 27 additions and 24 deletions
|
@ -75,11 +75,10 @@ func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base {
|
|||
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.specs = append(act.specs, spec)
|
||||
cfg.actions = append(cfg.actions, act)
|
||||
return cfg
|
||||
}
|
||||
|
||||
func MakeBase(name string, starter lib.StartProc) *Base {
|
||||
|
|
|
@ -73,7 +73,7 @@ func Forward(act lib.Action) bool {
|
|||
msg := act.Message()
|
||||
for _, rcvr := range act.Spec().Receivers() {
|
||||
addr := message.SimpleAddress(rcvr)
|
||||
message.Send(ctx, addr, msg)
|
||||
lib.Send(ctx, addr, msg)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
10
lib/lib.go
10
lib/lib.go
|
@ -25,7 +25,7 @@ type Config interface {
|
|||
Actions() []ActionConfig
|
||||
Children() []Config
|
||||
Add(Config)
|
||||
WithAction(string, ActionSpec) Config
|
||||
AddAction(string, ActionSpec)
|
||||
}
|
||||
|
||||
type Services map[string]Context
|
||||
|
@ -69,7 +69,7 @@ type StepProc = func(Context) bool
|
|||
type MessageHandler = func(Context, Message) bool
|
||||
type ActionHandler = func(Action) bool
|
||||
|
||||
// async Runners
|
||||
// library functions
|
||||
|
||||
func RunCtx(ctx Context, fct Proc) {
|
||||
ctx.WaitGroup().Add(1)
|
||||
|
@ -78,3 +78,9 @@ func RunCtx(ctx Context, fct Proc) {
|
|||
fct(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
func Send(ctx Context, addr Address, msg Message) {
|
||||
if srv, ok := ctx.Services()[addr.Service()]; ok {
|
||||
srv.Mailbox() <- msg
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ func (addr *address) Service() string {
|
|||
}
|
||||
|
||||
func (addr *address) Send(ctx lib.Context, msg lib.Message) {
|
||||
Send(ctx, addr, msg)
|
||||
lib.Send(ctx, addr, msg)
|
||||
}
|
||||
|
||||
func SimpleAddress(srv string) *address {
|
||||
|
@ -39,11 +39,3 @@ func SimpleAddress(srv string) *address {
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,20 +12,26 @@ import (
|
|||
func Config() lib.Config {
|
||||
ovr := Overrides().Use
|
||||
b := config.MakeBase
|
||||
|
||||
cfg := app.Cfg{
|
||||
Base: b("testing", testing.Start).
|
||||
WithAction("demo",
|
||||
action.BaseSpec(action.Forward,
|
||||
[]string{"test-receiver"})).(*config.Base),
|
||||
Base: b("testing", testing.Start),
|
||||
Home: ovr(".", HOME),
|
||||
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),
|
||||
ConfigFormat: "etc",
|
||||
})
|
||||
cfg.Add(b("test-receiver", core.Start).
|
||||
WithAction("*", action.BaseSpec(AH_Receiver, nil)))
|
||||
}
|
||||
cfg.Add(cfg_config)
|
||||
|
||||
cfg_test_rcvr := b("test-receiver", core.Start)
|
||||
cfg_test_rcvr.AddAction("*", action.BaseSpec(AH_Receiver, nil))
|
||||
cfg.Add(cfg_test_rcvr)
|
||||
|
||||
return &cfg
|
||||
}
|
||||
|
||||
|
|
|
@ -36,9 +36,9 @@ func SendTest(t *testing.T) {
|
|||
ctx := t.Ctx
|
||||
rcvr := message.SimpleAddress("testing")
|
||||
msg := message.StrMessage("demo")
|
||||
rcvr.Send(ctx, msg)
|
||||
lib.Send(ctx, rcvr, msg)
|
||||
rcvr = message.SimpleAddress("test-receiver")
|
||||
rcvr.Send(ctx, msg)
|
||||
lib.Send(ctx, rcvr, msg)
|
||||
}
|
||||
|
||||
// action handlers
|
||||
|
|
Loading…
Add table
Reference in a new issue