move appcontext implementation to separate source file

This commit is contained in:
Helmut Merz 2023-06-04 13:39:08 +02:00
parent 34ee4641dc
commit 2a188cfa1a
2 changed files with 53 additions and 48 deletions

53
lib/context/appcontext.go Normal file
View file

@ -0,0 +1,53 @@
package context
import (
"sync"
"git.sr.ht/~cco/go-scopes/lib"
)
type appContext struct {
*context
services Services
waitgroup *sync.WaitGroup
doneCh chan struct{}
}
func (ctx *appContext) ChildContext(cfg lib.Config) Context {
cctx := makeCtx(cfg, ctx)
ctx.services[cfg.Name()] = cctx
ctx.children = append(ctx.children, cctx)
return cctx
}
func (ctx *appContext) Services() Services {
return ctx.services
}
func (ctx *appContext) WithState(state ContextState) Context {
ctx.state = state
return ctx
}
func (ctx *appContext) WaitGroup() *sync.WaitGroup {
return ctx.waitgroup
}
func (ctx *appContext) Done() <-chan struct{} {
return ctx.doneCh
}
func (ctx *appContext) Stop() {
close(ctx.doneCh)
}
func AppContext(cfg lib.Config) Context {
ctx := &appContext{
context: makeCtx(cfg, nil),
services: Services{},
waitgroup: &sync.WaitGroup{},
doneCh: make(chan struct{}),
}
ctx.services[cfg.Name()] = ctx
return ctx
}

View file

@ -72,54 +72,6 @@ func makeCtx(cfg lib.Config, parent Context) *context {
}
}
// top-level application context
type appContext struct {
*context
services Services
waitgroup *sync.WaitGroup
doneCh chan struct{}
}
func (ctx *appContext) ChildContext(cfg lib.Config) Context {
cctx := makeCtx(cfg, ctx)
ctx.services[cfg.Name()] = cctx
ctx.children = append(ctx.children, cctx)
return cctx
}
func (ctx *appContext) Services() Services {
return ctx.services
}
func (ctx *appContext) WithState(state ContextState) Context {
ctx.state = state
return ctx
}
func (ctx *appContext) WaitGroup() *sync.WaitGroup {
return ctx.waitgroup
}
func (ctx *appContext) Done() <-chan struct{} {
return ctx.doneCh
}
func (ctx *appContext) Stop() {
close(ctx.doneCh)
}
func AppContext(cfg lib.Config) Context {
ctx := &appContext{
context: makeCtx(cfg, nil),
services: Services{},
waitgroup: &sync.WaitGroup{},
doneCh: make(chan struct{}),
}
ctx.services[cfg.Name()] = ctx
return ctx
}
// implement interface context.Context from standard library
func (ctx *context) Deadline() (deadline time.Time, ok bool) {