summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-runners/context.go
blob: edb69506037a3d7f16338e4dbbc4b6677eef5967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package runners

import (
	"context"
	"time"
)

// ContextWithCancel returns a new context.Context impl with cancel.
func ContextWithCancel() (context.Context, context.CancelFunc) {
	ctx := make(cancelctx)
	return ctx, func() { close(ctx) }
}

// 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
}