summaryrefslogtreecommitdiffstats
path: root/val.go
blob: 2b762cbcc5dc38e84cbfec04f940d740a86e9584 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package share

// Val is a concurrent interface to a piece of shared data.
//
// A client can read the data by sending a channel via request, and the stored value will
// be sent back via the channel. The client is responsible for closing the channel.
//
// The stored value can be changed by sending the new value via Set. Requests block until
// the first value is received on Set.
//
// Val should be closed after use.
type Val[T any] struct {
	Request chan<- chan T
	Set     chan<- T
}

func NewVal[T any]() Val[T] {
	request := make(chan chan T)
	set := make(chan T)
	go func() {
		val := <-set // wait for initial value
		for {
			select {
			case v, ok := <-set:
				if !ok { // closed
					return
				}
				val = v
			case req, ok := <-request:
				if !ok { // closed
					return
				}
				go func() { // don't wait for client to receive
					req <- val
				}()
			}
		}
	}()
	return Val[T]{request, set}
}

// Get makes a synchronous request and returns the stored value.
func (v Val[T]) Get() T {
	c := make(chan T)
	defer close(c)
	v.Request <- c
	return <-c
}

// TryGet returns the stored value if it has already been set, or false if it hasn't.
func (v Val[T]) TryGet() (*T, bool) {
	c := make(chan T)
	defer close(c)
	select {
	case v.Request <- c:
		val := <-c
		return &val, true
	default:
		return nil, false
	}
}

func (v Val[T]) Close() {
	close(v.Request)
	close(v.Set)
}