go-scopes/core/context/appcontext.go

55 lines
1 KiB
Go

package context
import (
"sync"
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/logging"
)
type appContext struct {
*context
services Services
logger *logging.Logger
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
}