-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
future.go
183 lines (157 loc) · 3.31 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
182
183
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]{
cb: cb,
cancelCb: func() {},
done: make(chan struct{}),
}
future.active()
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.Mutex
cb func(func(T), func(error))
cancelCb func()
next *Future[T]
done chan struct{}
result Result[T]
}
func (f *Future[T]) active() {
go f.cb(f.resolve, f.reject)
}
func (f *Future[T]) activeSync() {
f.cb(f.resolve, f.reject)
}
func (f *Future[T]) resolve(value T) {
f.mu.Lock()
defer f.mu.Unlock()
f.result = Ok(value)
if f.next != nil {
f.next.activeSync()
}
close(f.done)
}
func (f *Future[T]) reject(err error) {
f.mu.Lock()
defer f.mu.Unlock()
f.result = Err[T](err)
if f.next != nil {
f.next.activeSync()
}
close(f.done)
}
// Then 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()
f.next = &Future[T]{
cb: func(resolve func(T), reject func(error)) {
if f.result.IsError() {
reject(f.result.Error())
return
}
newValue, err := cb(f.result.MustGet())
if err != nil {
reject(err)
return
}
resolve(newValue)
},
cancelCb: func() {
f.Cancel()
},
done: make(chan struct{}),
}
select {
case <-f.done:
f.next.active()
default:
}
return f.next
}
// 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()
f.next = &Future[T]{
cb: func(resolve func(T), reject func(error)) {
if f.result.IsOk() {
resolve(f.result.MustGet())
return
}
newValue, err := cb(f.result.Error())
if err != nil {
reject(err)
return
}
resolve(newValue)
},
cancelCb: func() {
f.Cancel()
},
done: make(chan struct{}),
}
select {
case <-f.done:
f.next.active()
default:
}
return f.next
}
// 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()
f.next = &Future[T]{
cb: func(resolve func(T), reject func(error)) {
newValue, err := cb(f.result.Get())
if err != nil {
reject(err)
return
}
resolve(newValue)
},
cancelCb: func() {
f.Cancel()
},
done: make(chan struct{}),
}
select {
case <-f.done:
f.next.active()
default:
}
return f.next
}
// 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) {
<-f.done
return f.result.Get()
}
// Result wraps Collect and returns a Result.
func (f *Future[T]) Result() Result[T] {
return TupleToResult(f.Collect())
}
// Either wraps Collect and returns a Either.
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)
}