aboutsummaryrefslogtreecommitdiff
path: root/stmutil/context.go
blob: 75e3f8a57739d3b8fc99e52c2a4b6645bedd2691 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package stmutil

import (
	"context"

	"github.com/lukechampine/stm"
)

func ContextDoneVar(ctx context.Context) (*stm.Var, func()) {
	if ctx.Err() != nil {
		return stm.NewVar(true), func() {}
	}
	ctx, cancel := context.WithCancel(ctx)
	_var := stm.NewVar(false)
	go func() {
		<-ctx.Done()
		stm.AtomicSet(_var, true)
	}()
	return _var, cancel
}