diff --git a/app/app.go b/app/app.go index 5e49f2a..0018a52 100644 --- a/app/app.go +++ b/app/app.go @@ -8,6 +8,7 @@ import ( "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/lib" + "git.sr.ht/~cco/go-scopes/lib/message" ) type Cfg struct { @@ -41,13 +42,15 @@ func step(ctx lib.Context, sig <-chan os.Signal) bool { //ctx.LogInfo("Dispatcher interrupted", m.Map{}) return false case msg := <-ctx.Mailbox(): - fmt.Println("message", msg) + fmt.Println("app.step: message =", msg) //ctx.LogDebug("dispatcherStep", m.Map{"msg": msg}) - if msg.Action() == "quit" { + if msg == message.Quit { //ctx.LogInfo("Dispatcher stopped", m.Map{}) return false } return ctx.HandleMsg(msg) + case <-ctx.Done(): + return false } return true } diff --git a/config/config.go b/config/config.go index b118c6a..454e216 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,8 @@ type Base struct { children []lib.Config } +// lib.Config implementation + func (cfg *Base) Name() string { return cfg.name } @@ -42,11 +44,6 @@ func (cfg *Base) MessageHandler() lib.MessageHandler { return cfg.msgHandler } -func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base { - cfg.msgHandler = hdlr - return cfg -} - func (cfg *Base) Actions() []lib.ActionConfig { return cfg.actions } @@ -59,6 +56,18 @@ func (cfg *Base) Add(child lib.Config) { cfg.children = append(cfg.children, child) } +// implementation-specific methods and functions + +func (cfg *Base) WithStep(step lib.StepProc) *Base { + cfg.step = step + return cfg +} + +func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base { + cfg.msgHandler = hdlr + return cfg +} + func MakeBase(name string, starter lib.StartProc) *Base { return &Base{ name: name, diff --git a/lib/message/message.go b/lib/message/message.go index 6bae981..e3a46f3 100644 --- a/lib/message/message.go +++ b/lib/message/message.go @@ -18,6 +18,8 @@ func StrMessage(action string) strMessage { return strMessage(action) } +var Quit = StrMessage("quit") + // Address implementation type address struct { diff --git a/testing/testing.go b/testing/testing.go index f8b210b..3217e4b 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -50,7 +50,7 @@ func (t *T) TearDownApp() { // give actors time to recieve all messages: time.Sleep(100 * time.Millisecond) //t.Check() - t.Ctx.Services()["testing"].Mailbox() <- message.StrMessage("quit") + t.Ctx.Services()["testing"].Mailbox() <- message.Quit fmt.Println("teardown ", t.Ctx.WaitGroup()) t.Ctx.WaitGroup().Wait() //t.AssertNoUncheckedMessages()