Repository navigation
Expand file tree
/
Copy pathcallback.go
More file actions
75 lines (66 loc) · 1.71 KB
/
Copy pathcallback.go
File metadata and controls
75 lines (66 loc) · 1.71 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package workmanager
import (
"sync"
)
func newCallbackController() *callbackController {
return &callbackController{callbacks: make(map[WorkStep]*callbacks)}
}
type callbackController struct {
mu sync.RWMutex
callbacks map[WorkStep]*callbacks
}
// GetCallbacks query specified step's callbacks, if not found, build a new one and return it
func (ctr *callbackController) GetCallbacks(step WorkStep) *callbacks {
c := ctr.getCallbacks(step)
if c == nil {
c = new(callbacks)
ctr.setCallback(step, c)
}
return c
}
func (ctr *callbackController) CallbackStatus(step WorkStep) (beforeHookCount, afterHookCount int) {
if callbacks := ctr.getCallbacks(step); callbacks == nil {
return callbacks.Status()
}
return
}
func (ctr *callbackController) getCallbacks(step WorkStep) *callbacks {
ctr.mu.RLock()
defer ctr.mu.RUnlock()
return ctr.callbacks[step]
}
func (ctr *callbackController) setCallback(step WorkStep, callbacks *callbacks) {
ctr.mu.Lock()
defer ctr.mu.Unlock()
ctr.callbacks[step] = callbacks
}
type callbacks struct {
mu sync.RWMutex
before []StepCallback
after []StepCallback
}
func (c *callbacks) RegisterBefore(calls ...StepCallback) {
c.mu.Lock()
defer c.mu.Unlock()
c.before = append(c.before, calls...)
}
func (c *callbacks) RegisterAfter(calls ...StepCallback) {
c.mu.Lock()
defer c.mu.Unlock()
c.after = append(c.after, calls...)
}
func (c *callbacks) BeforeWork() []StepCallback {
c.mu.RLock()
defer c.mu.RUnlock()
return c.before
}
func (c *callbacks) AfterWork() []StepCallback {
c.mu.RLock()
defer c.mu.RUnlock()
return c.after
}
func (c *callbacks) Status() (beforeHookCount, afterHookCount int) {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.before), len(c.after)
}