-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasync.go
106 lines (93 loc) · 2.49 KB
/
async.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
94
95
96
97
98
99
100
101
102
103
104
105
106
package async // import "code.nkcmr.net/async"
import (
"context"
)
// Promise is an abstract representation of a value that might eventually be
// delivered.
type Promise[T any] interface {
// Settled indicates if a call to Await will cause a blocking behavior, or
// if the result will be immediately returned.
Settled() bool
// Await will cause the calling code to block and wait for the promise to
// settle. Await MUST be able to be called by multiple goroutines and safely
// deliver the same value/error to all waiting goroutines. Successive calls
// to Await should continue to respond with the result even once the promise
// is settled.
Await(context.Context) (T, error)
}
type syncPromise[T any] struct {
done chan struct{}
v T
err error
}
func (s *syncPromise[T]) Await(ctx context.Context) (T, error) {
select {
case <-ctx.Done():
var zerov T
return zerov, ctx.Err()
case <-s.done:
return s.v, s.err
}
}
func (s *syncPromise[T]) Settled() bool {
select {
case <-s.done:
return true
default:
return false
}
}
// NewPromise wraps a function in a goroutine that will make the result of that
// function deliver its result to the holder of the promise.
func NewPromise[T any](fn func() (T, error)) Promise[T] {
c := &syncPromise[T]{
done: make(chan struct{}),
}
go func() {
c.v, c.err = fn()
close(c.done)
}()
return c
}
type rp[T any] struct {
v T
err error
}
func (r *rp[T]) Settled() bool { return true }
func (r *rp[T]) Await(context.Context) (T, error) {
return r.v, r.err
}
// Resolve wraps a value in a promise that will always be immediately settled
// and return the provided value.
func Resolve[T any](v T) Promise[T] {
return &rp[T]{v: v}
}
// Reject wraps an error in a promise that will always be immediately settled
// and return an error.
func Reject[T any](err error) Promise[T] {
return &rp[T]{err: err}
}
// All takes a slice of promises and will await the result of all of the
// specified promises. If any promise should return an error, the wh
func All[T any](ctx context.Context, promises []Promise[T]) ([]T, error) {
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
defer cancel()
out := make([]T, len(promises))
errc := make(chan error, len(out))
waiter := func(i int, p Promise[T]) {
var err error
out[i], err = p.Await(ctx)
errc <- err
}
for i := range out {
go waiter(i, promises[i])
}
for i := 0; i < len(out); i++ {
if err := <-errc; err != nil {
cancel()
return nil, err
}
}
return out, nil
}