minor renamings; use generic functions for access to state and config data via type assertions

This commit is contained in:
Helmut Merz 2023-06-29 17:39:48 +02:00
parent fb423d795d
commit bed55a63a2
4 changed files with 20 additions and 13 deletions

View file

@ -91,17 +91,17 @@ func Base(name string, starter lib.StartProc) *base {
return &base{ return &base{
name: name, name: name,
starter: starter, starter: starter,
step: Step, step: DefaultStep,
msgHandler: MsgHandler, msgHandler: DefaultMsgHandler,
doneHandler: DoneHandler, doneHandler: DefaultDoneHandler,
actions: nil, actions: nil,
} }
} }
// will be set by core.init() // will be set by core.init()
var Step lib.StepProc var DefaultStep lib.StepProc
var MsgHandler lib.MessageHandler var DefaultMsgHandler lib.MessageHandler
var DoneHandler lib.StepProc var DefaultDoneHandler lib.StepProc
// action configuration // action configuration

View file

@ -44,7 +44,7 @@ func HandleDone(ctx lib.Context) bool {
} }
func init() { func init() {
config.Step = Step // avoid import cycle config.DefaultStep = Step // avoid import cycle
config.MsgHandler = HandleMessage config.DefaultMsgHandler = HandleMessage
config.DoneHandler = HandleDone config.DefaultDoneHandler = HandleDone
} }

View file

@ -88,3 +88,11 @@ func Send(ctx Context, addr Address, msg Message) {
srv.Mailbox() <- msg srv.Mailbox() <- msg
} }
} }
func GetState[St ContextState](ctx Context) St {
return ctx.State().(St)
}
func GetCfg[C Config](ctx Context) C {
return ctx.Config().(C)
}

View file

@ -19,14 +19,13 @@ type ServerState struct {
} }
func Start(ctx lib.Context) { func Start(ctx lib.Context) {
ctx.Config().(*Cfg).BaseCfg.WithDoneHandler(HandleDone) lib.GetCfg[*Cfg](ctx).BaseCfg.WithDoneHandler(HandleDone)
Serve(ctx) Serve(ctx)
lib.RunCtx(ctx, core.Listen) lib.RunCtx(ctx, core.Listen)
} }
func HandleDone(ctx lib.Context) bool { func HandleDone(ctx lib.Context) bool {
state := ctx.State().(ServerState) lib.GetState[*ServerState](ctx).server.Shutdown(ctx)
state.server.Shutdown(ctx)
return false return false
} }
@ -39,7 +38,7 @@ func Serve(ctx lib.Context) {
Addr: ":8123", Addr: ":8123",
Handler: r, Handler: r,
} }
ctx.WithState(ServerState{ ctx.WithState(&ServerState{
server: srv, server: srv,
}) })
lib.RunCtx(ctx, func(ctx lib.Context) { lib.RunCtx(ctx, func(ctx lib.Context) {