forked from samber/mo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
future.go
181 lines (144 loc) · 3.01 KB
/
future.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package mo
import (
"sync"
)
// NewFuture instanciate a new future.
func NewFuture[T any](cb func(resolve func(T), reject func(error))) *Future[T] {
future := Future[T]{
mu: sync.RWMutex{},
next: nil,
cancelCb: func() {},
}
go func() {
cb(future.resolve, future.reject)
}()
return &future
}
// Future represents a value which may or may not currently be available, but will be
// available at some point, or an exception if that value could not be made available.
type Future[T any] struct {
mu sync.RWMutex
next func(T, error)
cancelCb func()
}
func (f *Future[T]) resolve(value T) {
f.mu.RLock()
defer f.mu.RUnlock()
if f.next != nil {
f.next(value, nil)
}
}
func (f *Future[T]) reject(err error) {
f.mu.RLock()
defer f.mu.RUnlock()
if f.next != nil {
f.next(empty[T](), err)
}
}
// Catch is called when Future is resolved. It returns a new Future.
func (f *Future[T]) Then(cb func(T) (T, error)) *Future[T] {
f.mu.Lock()
defer f.mu.Unlock()
future := &Future[T]{
mu: sync.RWMutex{},
next: nil,
cancelCb: func() {
f.Cancel()
},
}
f.next = func(value T, err error) {
if err != nil {
future.reject(err)
return
}
newValue, err := cb(value)
if err != nil {
future.reject(err)
return
}
future.resolve(newValue)
}
return future
}
// Catch is called when Future is rejected. It returns a new Future.
func (f *Future[T]) Catch(cb func(error) (T, error)) *Future[T] {
f.mu.Lock()
defer f.mu.Unlock()
future := &Future[T]{
mu: sync.RWMutex{},
next: nil,
cancelCb: func() {
f.Cancel()
},
}
f.next = func(value T, err error) {
if err == nil {
future.resolve(value)
return
}
newValue, err := cb(err)
if err != nil {
future.reject(err)
return
}
future.resolve(newValue)
}
return future
}
// Finally is called when Future is processed either resolved or rejected. It returns a new Future.
func (f *Future[T]) Finally(cb func(T, error) (T, error)) *Future[T] {
f.mu.Lock()
defer f.mu.Unlock()
future := &Future[T]{
mu: sync.RWMutex{},
next: nil,
cancelCb: func() {
f.Cancel()
},
}
f.next = func(value T, err error) {
newValue, err := cb(value, err)
if err != nil {
future.reject(err)
return
}
future.resolve(newValue)
}
return future
}
// Cancel cancels the Future chain.
func (f *Future[T]) Cancel() {
f.mu.Lock()
defer f.mu.Unlock()
f.next = nil
if f.cancelCb != nil {
f.cancelCb()
}
}
// Collect awaits and return result of the Future.
func (f *Future[T]) Collect() (T, error) {
done := make(chan struct{})
var a T
var b error
f.mu.Lock()
f.next = func(value T, err error) {
a = value
b = err
done <- struct{}{}
}
f.mu.Unlock()
<-done
return a, b
}
// Result wraps Collect and returns a Result.
func (f *Future[T]) Result() Result[T] {
return TupleToResult(f.Collect())
}
// Result wraps Collect and returns a Result.
func (f *Future[T]) Either() Either[error, T] {
v, err := f.Collect()
if err != nil {
return Left[error, T](err)
}
return Right[error, T](v)
}