diff --git a/lib/context/appcontext.go b/lib/context/appcontext.go new file mode 100644 index 0000000..719999d --- /dev/null +++ b/lib/context/appcontext.go @@ -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 +} diff --git a/lib/context/context.go b/lib/context/context.go index efcd562..d0ec5b8 100644 --- a/lib/context/context.go +++ b/lib/context/context.go @@ -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) {