minor renamings; use generic functions for access to state and config data via type assertions
This commit is contained in:
parent
fb423d795d
commit
bed55a63a2
4 changed files with 20 additions and 13 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue