-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.go
56 lines (45 loc) · 807 Bytes
/
funcs.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
package workflow
import (
"context"
"math"
"sync"
)
const abortIndex int8 = math.MaxInt8 / 2
type Event = func(ctx context.Context, data *WorkData) error
type funcs struct {
mux sync.Mutex
fs []Event
index int8
}
func NewFuncs() *funcs {
return &funcs{
fs: []Event{},
index: -1,
}
}
func (fs *funcs) Add(f Event) {
fs.mux.Lock()
defer fs.mux.Unlock()
fs.fs = append(fs.fs, f)
}
func (fs *funcs) next(ctx context.Context, data *WorkData) error {
fs.index++
for fs.index < int8(len(fs.fs)) {
err := fs.fs[fs.index](ctx, data)
if err != nil {
fs.abort()
return err
}
fs.index++
}
return nil
}
func (fs *funcs) isAborted() bool {
return fs.index >= abortIndex
}
func (fs *funcs) abort() {
fs.index = abortIndex
}
func (fs *funcs) reset() {
fs.index = -1
}