package context import ( stdlib_context "context" "fmt" "sync" "time" "git.sr.ht/~cco/go-scopes/lib" ) type Context = lib.Context type ContextState = lib.ContextState type Services = lib.Services type context struct { cfg lib.Config parent Context children []Context state ContextState mailbox chan lib.Message } func (ctx *context) Config() lib.Config { return ctx.cfg } func (ctx *context) Parent() lib.Context { return ctx.parent } func (ctx *context) Services() Services { return ctx.parent.Services() } func (ctx *context) ChildContext(cfg lib.Config) Context { cctx := makeCtx(cfg, ctx) ctx.Services()[cfg.Name()] = cctx ctx.children = append(ctx.children, cctx) return cctx } func (ctx *context) State() ContextState { return ctx.state } func (ctx *context) WithState(state ContextState) Context { ctx.state = state return ctx } func (ctx *context) Mailbox() chan lib.Message { return ctx.mailbox } func (ctx *context) HandleMsg(msg lib.Message) bool { fmt.Println("HandleMsg ", msg) // return ctx.Config().MessageHandler()(ctx, msg) return true } func (ctx *context) WaitGroup() *sync.WaitGroup { return ctx.parent.WaitGroup() } func (ctx *context) Done() <-chan struct{} { return ctx.parent.Done() } func (ctx *context) Stop() { // ctx.Warn("Only application-level service can be stopped } func makeCtx(cfg lib.Config, parent Context) *context { return &context{ cfg: cfg, parent: parent, mailbox: make(chan lib.Message, 10), } } // implement interface context.Context from standard library func (ctx *context) Deadline() (deadline time.Time, ok bool) { return time.Time{}, false } func (ctx *context) Err() error { select { case _, ok := <-ctx.Done(): if !ok { return stdlib_context.Canceled } default: return nil } return nil } func (ctx *context) Value(key interface{}) interface{} { return nil }