package apicontext import ( "context" "time" ) type Context struct { ctx context.Context cancel context.CancelFunc } func ContextIsDone(ctx context.Context) bool { select { case <-ctx.Done(): return true default: return false } } func ContextIsNotDone(ctx context.Context) bool { return !ContextIsDone(ctx) } func Background() context.Context { return context.Background() } func (c *Context) Cancel() { if c.cancel != nil { c.cancel() } } func (c *Context) Deadline() (deadline time.Time, ok bool) { return c.ctx.Deadline() } func (c *Context) Done() <-chan struct{} { return c.ctx.Done() } func (c *Context) Err() error { return c.ctx.Err() } func (c *Context) Value(key any) any { return c.ctx.Value(key) } func (c *Context) IsDone() bool { return ContextIsDone(c.ctx) } func (c *Context) IsNotDone() bool { return ContextIsNotDone(c.ctx) } // constructors func WithCancel(parent context.Context) *Context { ctx, cancel := context.WithCancel(parent) return &Context{ctx: ctx, cancel: cancel} } func WithTimeout(parent context.Context, timeout time.Duration) *Context { ctx, cancel := context.WithTimeout(parent, timeout) return &Context{ctx: ctx, cancel: cancel} } func WithDeadline(parent context.Context, deadline time.Time) *Context { ctx, cancel := context.WithDeadline(parent, deadline) return &Context{ctx: ctx, cancel: cancel} } func WithValue(parent context.Context, key, val any) *Context { ctx := context.WithValue(parent, key, val) return &Context{ctx: ctx} }