GoToSocial/vendor/codeberg.org/gruf/go-runners/context.go
dependabot[bot] 0a9874329d
[chore]: Bump codeberg.org/gruf/go-runners from 1.4.0 to 1.5.1 (#1428)
Bumps codeberg.org/gruf/go-runners from 1.4.0 to 1.5.1.

---
updated-dependencies:
- dependency-name: codeberg.org/gruf/go-runners
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-02-06 08:08:22 +00:00

65 lines
1.1 KiB
Go

package runners
import (
"context"
"time"
)
// closedctx is an always closed context.
var closedctx = func() context.Context {
ctx := make(chan struct{})
close(ctx)
return CancelCtx(ctx)
}()
// Closed returns an always closed context.
func Closed() context.Context {
return closedctx
}
// CtxWithCancel returns a new context.Context impl with cancel.
func CtxWithCancel() (context.Context, context.CancelFunc) {
ctx := make(chan struct{})
cncl := func() { close(ctx) }
return CancelCtx(ctx), cncl
}
// CancelCtx is the simplest possible cancellable context.
type CancelCtx (<-chan struct{})
func (CancelCtx) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (ctx CancelCtx) Done() <-chan struct{} {
return ctx
}
func (ctx CancelCtx) Err() error {
select {
case <-ctx:
return context.Canceled
default:
return nil
}
}
func (CancelCtx) Value(key interface{}) interface{} {
return nil
}
func (ctx CancelCtx) String() string {
var state string
select {
case <-ctx:
state = "closed"
default:
state = "open"
}
return "CancelCtx{state:" + state + "}"
}
func (ctx CancelCtx) GoString() string {
return "runners." + ctx.String()
}