-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathawait-value.go
50 lines (45 loc) · 1.59 KB
/
await-value.go
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
/*
© 2024-present Harald Rudell <haraldrudell@proton.me> (https://haraldrudell.github.io/haraldrudell/)
All rights reserved
*/
package parl
// AwaitValue awaits value or close, blocking until either event
// - hasValue true: value is valid, possibly the zero-value like
// a nil interface value
// - hasValue: false: the stream is closable and closed
// - stream: an awaitable possibly closable source type like [Source1]
// - — stream’s DataWaitCh Get and if present EmptyCh methods are used
// - — stream cannot be eg. [AtomicError] because it is not awaitable
// - AwaitValue wraps a 10-line read operation as a two-value expression
func AwaitValue[T any](stream Source1[T]) (value T, hasValue bool) {
// endCh is a possible close channel
// - nil if not closable
var endCh AwaitableCh
if closable, isClosable := stream.(Closable[T]); isClosable {
endCh = closable.EmptyCh(CloseAwaiter)
}
// loop until value or closed
for {
select {
case <-endCh:
return // closable is closed return: hasValue false
case <-stream.DataWaitCh():
// competing with other threads for values
// - may receive nothing
if value, hasValue = stream.Get(); hasValue {
return // value read return: hasValue true, value valid
}
}
}
}
// IsClosed returns true if closable is closed or triggered
// - isClosed is a single boolean value usable with for or if
// - IsClosed wraps a 6-line read into a single-value boolean expression
func IsClosed[T any](closable Closable[T]) (isClosed bool) {
select {
case <-closable.EmptyCh(CloseAwaiter):
isClosed = true
default:
}
return
}