-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.go
61 lines (52 loc) · 1.36 KB
/
hook.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
package lu
import (
"context"
"fmt"
"sort"
)
type hook struct {
Name string
createOrder int
Priority HookPriority
// F is called either at the start or at the end of the application lifecycle
// ctx will be cancelled if the function takes too long
F func(ctx context.Context) error
}
func sortHooks(h []hook) {
sort.Slice(h, func(i, j int) bool {
hi, hj := h[i], h[j]
if hi.Priority != hj.Priority {
return hi.Priority < hj.Priority
}
return hi.createOrder < hj.createOrder
})
}
type HookOption func(*hook)
func applyHookOptions(h *hook, opts []HookOption) {
for _, o := range opts {
o(h)
}
}
// WithHookName is used for logging so each Hook can be identified
func WithHookName(s string) HookOption {
return func(options *hook) {
options.Name = s
}
}
type HookPriority int
const (
HookPriorityFirst HookPriority = -100
HookPriorityDefault HookPriority = 0
HookPriorityLast HookPriority = 100
)
// WithHookPriority controls the order in which hooks are run, the lower the value of p
// the earlier it will be run (compared to other hooks)
// The default priority is 0, negative priorities will be run before positive ones
func WithHookPriority(p HookPriority) HookOption {
if p < HookPriorityFirst || p > HookPriorityLast {
panic(fmt.Sprintln("invalid hook priority", p))
}
return func(options *hook) {
options.Priority = p
}
}