blob: 584544a3da98a462383e2c6155867101b42e13ee (
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
|
package gui
import "time"
const timeout = 1 * time.Second
// trySend returns true if v can be sent to c within timeout, or false otherwise.
func trySend[T any](c chan<- T, v T, timeout time.Duration) bool {
timer := time.NewTimer(timeout)
select {
case c <- v:
return true
case <-timer.C:
return false
}
}
// tryRecv returns the value received from c, or false if no value is received within timeout.
func tryRecv[T any](c <-chan T, timeout time.Duration) (*T, bool) {
timer := time.NewTimer(timeout)
select {
case v := <-c:
return &v, true
case <-timer.C:
return nil, false
}
}
|