-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
148 lines (129 loc) · 2.58 KB
/
progress.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
package promise
import (
"context"
"fmt"
"sync"
)
type Progress[T any] interface {
Cancel()
Await() (T, error)
await() (T, error)
getFulfilmentChannel() chan Output[T]
abandon(err error)
leave(err error)
eliminate()
}
type progress[T any] struct {
doOnResolved func(T)
doOnRejected func(error)
doOnCanceled func()
doOnTimedOut func()
doFinally func(event)
fulfilmentChannel chan Output[T]
context context.Context
cancel context.CancelFunc
key interface{}
doneOnce sync.Once
}
func (p *progress[T]) Cancel() {
defer p.cancel()
}
func (p *progress[T]) Await() (T, error) {
return p.await()
}
func (p *progress[T]) await() (T, error) {
var z T
select {
case <-p.context.Done():
var e error
switch p.context.Err() {
case context.Canceled:
e = canceledError("manually canceled")
p.abandon(e)
case context.DeadlineExceeded:
e = timedOutError("timed-out")
p.leave(e)
}
return z, e
case r, o := <-p.fulfilmentChannel:
defer p.cancel()
if o {
defer close(p.fulfilmentChannel)
}
if r == nil {
return z, nil
}
return r.Payload(), r.Error()
}
}
func (p *progress[T]) getFulfilmentChannel() chan Output[T] {
return p.fulfilmentChannel
}
func (p *progress[T]) getCancelContext() context.Context {
return p.context
}
func (p *progress[T]) getCancelFunction() context.CancelFunc {
return p.cancel
}
func (p *progress[T]) abandon(err error) {
p.doneOnce.Do(func() {
if p.doOnCanceled != nil {
p.doOnCanceled()
}
if p.doFinally != nil {
p.doFinally(EventCanceled)
}
})
}
func (p *progress[T]) leave(err error) {
p.doneOnce.Do(func() {
if p.doOnTimedOut != nil {
p.doOnTimedOut()
}
if p.doFinally != nil {
p.doFinally(EventTimedOut)
}
})
}
func (p *progress[T]) eliminate() {
p.doneOnce.Do(func() {
if p.doFinally != nil {
p.doFinally(EventEliminated)
}
})
}
func (p *progress[T]) handleProbablePanic() {
e := recover()
if e == nil {
return
}
switch v := e.(type) {
case error:
p.reject(v)
default:
p.reject(fmt.Errorf("%+v", v))
}
}
func (p *progress[T]) resolve(pay T) {
p.doneOnce.Do(func() {
p.fulfilmentChannel <- newOutput[T](pay, nil).setKey(p.key)
if p.doOnResolved != nil {
p.doOnResolved(pay)
}
if p.doFinally != nil {
p.doFinally(EventResolved)
}
})
}
func (p *progress[T]) reject(err error) {
p.doneOnce.Do(func() {
var z T
p.fulfilmentChannel <- newOutput[T](z, err).setKey(p.key)
if p.doOnRejected != nil {
p.doOnRejected(err)
}
if p.doFinally != nil {
p.doFinally(EventRejected)
}
})
}