-
Notifications
You must be signed in to change notification settings - Fork 41
/
Invokeresult.go
45 lines (42 loc) · 1.12 KB
/
Invokeresult.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
package signalr
import (
"context"
)
// InvokeResult is the combined value/error result for async invocations. Used as channel type.
type InvokeResult struct {
Value interface{}
Error error
}
// newInvokeResultChan combines a value result and an error result channel into one InvokeResult channel
// The InvokeResult channel is automatically closed when both input channels are closed.
func newInvokeResultChan(ctx context.Context, resultChan <-chan interface{}, errChan <-chan error) <-chan InvokeResult {
ch := make(chan InvokeResult, 1)
go func(ctx context.Context, ch chan InvokeResult, resultChan <-chan interface{}, errChan <-chan error) {
var resultChanClosed, errChanClosed bool
loop:
for !resultChanClosed || !errChanClosed {
select {
case <-ctx.Done():
break loop
case value, ok := <-resultChan:
if !ok {
resultChanClosed = true
} else {
ch <- InvokeResult{
Value: value,
}
}
case err, ok := <-errChan:
if !ok {
errChanClosed = true
} else {
ch <- InvokeResult{
Error: err,
}
}
}
}
close(ch)
}(ctx, ch, resultChan, errChan)
return ch
}