-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhook.go
More file actions
92 lines (79 loc) · 2.18 KB
/
hook.go
File metadata and controls
92 lines (79 loc) · 2.18 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2024, Peter Ohler, All rights reserved.
package slip
import "fmt"
var (
setHooks []*hook
unsetHooks []*hook
defunHooks []*hook
classHooks []*hook
)
type hook struct {
fun func(p *Package, key string)
id string
}
// AddSetHook adds a hook that is called after setting a variable or after a
// defvar or defparameter is called.
func AddSetHook(id string, fun func(p *Package, key string)) {
setHooks = append(setHooks, &hook{id: id, fun: fun})
}
// RemoveSetHook removes the set hook with the specified ID.
func RemoveSetHook(id string) {
for i, h := range setHooks {
if id == h.id {
setHooks = append(setHooks[:i], setHooks[i+1:]...)
return
}
}
}
// AddUnsetHook add a hook that is called when a variable is unset or removed.
func AddUnsetHook(id string, fun func(p *Package, key string)) {
unsetHooks = append(unsetHooks, &hook{id: id, fun: fun})
}
// RemoveUnsetHook removes the unset hook with the specified ID.
func RemoveUnsetHook(id string) {
for i, h := range unsetHooks {
if id == h.id {
unsetHooks = append(unsetHooks[:i], unsetHooks[i+1:]...)
return
}
}
}
// AddDefunHook add a hook that is called after a function is added.
func AddDefunHook(id string, fun func(p *Package, key string)) {
defunHooks = append(defunHooks, &hook{id: id, fun: fun})
}
// AddClassHook add a hook that is called after a class is added.
func AddClassHook(id string, fun func(p *Package, key string)) {
classHooks = append(classHooks, &hook{id: id, fun: fun})
}
// RemoveDefunHook removes the defun hook with the specified ID.
func RemoveDefunHook(id string) {
for i, h := range defunHooks {
if id == h.id {
defunHooks = append(defunHooks[:i], defunHooks[i+1:]...)
return
}
}
}
// RemoveClassHook removes the class hook with the specified ID.
func RemoveClassHook(id string) {
for i, h := range classHooks {
if id == h.id {
classHooks = append(classHooks[:i], classHooks[i+1:]...)
return
}
}
}
func callSetHooks(pkg *Package, name string) {
if pkg == nil {
for _, h := range setHooks {
h.fun(pkg, name)
}
} else {
pname := fmt.Sprintf("%s:%s", pkg.Name, name)
for _, h := range setHooks {
h.fun(pkg, name)
h.fun(pkg, pname)
}
}
}