-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrollback.go
More file actions
61 lines (50 loc) · 1.23 KB
/
rollback.go
File metadata and controls
61 lines (50 loc) · 1.23 KB
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
package gotcc
import "sync"
type undoStack struct {
lock sync.Mutex
items []*undoFunc
}
type undoFunc struct {
name string
skipError bool
args map[string]interface{}
f func(map[string]interface{}) error
}
func newUndoFunc(name string, skipError bool, undo func(args map[string]interface{}) error, args map[string]interface{}) *undoFunc {
return &undoFunc{
name: name,
skipError: skipError,
args: args,
f: undo,
}
}
func (u *undoStack) push(uf *undoFunc) {
u.lock.Lock()
u.items = append(u.items, uf)
u.lock.Unlock()
}
func (u *undoStack) reset() {
u.lock.Lock()
u.items = []*undoFunc{}
u.lock.Unlock()
}
func (u *undoStack) undoAll(taskErrors *errorLisk, cancelled *cancelList) *errorLisk {
undoErrors := &errorLisk{}
for i := len(u.items) - 1; i >= 0; i-- {
u.items[i].args["TASKERR"] = taskErrors.items
u.items[i].args["UNDOERR"] = undoErrors.items
u.items[i].args["CANCELLED"] = cancelled.items
err := u.items[i].f(u.items[i].args)
if err != nil {
undoErrors.append(newErrorMessage(u.items[i].name, err))
if !u.items[i].skipError {
return undoErrors
}
}
}
return undoErrors
}
// Default undo function
var EmptyUndoFunc = func(args map[string]interface{}) error {
return nil
}