-
Notifications
You must be signed in to change notification settings - Fork 3
/
resolvers.go
93 lines (71 loc) · 1.55 KB
/
resolvers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package go_promise
import (
"errors"
"strings"
)
type Errors []error
func (es Errors) Combine() error {
return errors.New(es.Error())
}
func (es Errors) Error() string {
errs := make([]string, 0, len(es))
for _, e := range es {
errs = append(errs, e.Error())
}
return strings.Join(errs, "\n\n")
}
type SettledResult[V any] struct {
Value V
Error error
}
func (r SettledResult[V]) IsRejected() bool {
return r.Error != nil
}
func (r SettledResult[V]) IsResolved() bool {
return r.Error == nil
}
type SettledResults[V any] []SettledResult[V]
func (rs SettledResults[V]) Values() []V {
values := make([]V, 0, len(rs))
for _, result := range rs {
if result.IsRejected() {
continue
}
values = append(values, result.Value)
}
return values
}
func (rs SettledResults[V]) Errors() Errors {
errs := make(Errors, 0, len(rs))
for _, result := range rs {
if result.IsResolved() {
continue
}
errs = append(errs, result.Error)
}
return errs
}
type settledResultChanel[V any] chan SettledResult[V]
func (c settledResultChanel[V]) empty() bool {
boolChan := make(chan bool)
defer close(boolChan)
go func() {
for range c {
}
boolChan <- true
}()
return <-boolChan
}
type ResolveFunc[V any] func(value V)
type RejectFunc func(err error)
type ExecuteFunc[V any] func(resolve ResolveFunc[V], reject RejectFunc)
func createResolveMethod[V any](valueChan chan V) ResolveFunc[V] {
return func(value V) {
valueChan <- value
}
}
func createRejectMethod(errChan chan error) RejectFunc {
return func(err error) {
errChan <- err
}
}