blob: 48cc3d3f11dcc374e42df81e683970ab4bdb894b (
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
|
package gui
// server is a concurrent interface that stores a value and serves it to clients.
// The stored value can be changed by sending the new value via update. server blocks
// until the first value is received.
//
// A client can read the stored value by sending a channel via request. The server
// responds by sending the value back via the channel. The client is responsible for
// closing the channel.
//
// A server should be closed after use.
type server[T any] struct {
request chan<- chan T
update chan<- T
}
func newServer[T any]() server[T] {
request := make(chan chan T)
update := make(chan T)
go func() {
val := <-update // wait for initial value
for {
select {
case v, ok := <-update:
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 server[T]{request, update}
}
// get makes a synchronous request to the server and returns the stored value.
func (srv server[T]) get() T {
c := make(chan T)
srv.request <- c
return <-c
}
func (srv server[T]) close() {
close(srv.request)
close(srv.update)
}
|