Skip to content

Commit

Permalink
Corrected Active() to take into account suspended hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
rasteric committed Mar 10, 2020
1 parent 64641fe commit ed535b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Remove all functions for the hook.

`Active(id int) bool`

Returns true if one or more functions for the hook are set, false otherwise. Using this function to check first may be more efficient than calling Exec directly, because of the arguments that Exec takes.
Returns true if one or more functions for the hook are set and the hook is not suspended, false otherwise. Using this function to check first may be more efficient than calling Exec directly, because of the arguments that Exec takes.

`WithHookSuspended(hook int, f func())`

Expand Down
14 changes: 12 additions & 2 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (h *HookContainer) suspend() {
h.suspended = true
}

// isSuspended returns true if the hook container is suspended, false otherwise.
func (h *HookContainer) isSuspended() bool {
h.mutex.RLock()
defer h.mutex.RUnlock()
return h.suspended
}

// unsuspend the hook container so that future calls to exec will call the procedures stored.
func (h *HookContainer) unsuspend() {
h.mutex.Lock()
Expand Down Expand Up @@ -126,8 +133,11 @@ func RemoveAll(hook int) {
func Active(hook int) bool {
lock.RLock()
defer lock.RUnlock()
_, ok := hooks[hook]
return ok
container, ok := hooks[hook]
if !ok || container == nil {
return false
}
return !container.isSuspended()
}

// WithHookSuspended ensures that within the function provided all calls to the hooks Exec function
Expand Down

0 comments on commit ed535b4

Please sign in to comment.