Skip to content

Commit

Permalink
refactor: hook
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 1, 2023
1 parent bddc97c commit d750912
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
16 changes: 7 additions & 9 deletions pkg/hook/builder.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package hook

type (
// Builder builds a new Hooks.
Builder []func(*Hook) error
)
// Builder builds a new Hooks.
type Builder []func(*Hook) error

// NewBuilder returns a new HooksBuilder.
func NewBuilder(funcs ...func(*Hook) error) Builder {
return Builder(funcs)
}

// AddToHooks adds all registered hook to s.
func (b *Builder) AddToHooks(h *Hook) error {
for _, f := range *b {
// AddToHooks adds all registered hooks to h.
func (b Builder) AddToHooks(h *Hook) error {
for _, f := range b {
if err := f(h); err != nil {
return err
}
}
return nil
}

// Register adds one or more hook.
// Register adds one or more hooks.
func (b *Builder) Register(funcs ...func(*Hook) error) {
*b = append(*b, funcs...)
}

// Build returns a new Hooks containing the registered hooks.
func (b *Builder) Build() (*Hook, error) {
func (b Builder) Build() (*Hook, error) {

Check warning on line 27 in pkg/hook/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/hook/builder.go#L27

Added line #L27 was not covered by tests
h := New()
if err := b.AddToHooks(h); err != nil {
return nil, err
Expand Down
24 changes: 11 additions & 13 deletions pkg/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,38 @@ import (
"github.com/siyul-park/uniflow/pkg/symbol"
)

type (
// Hook is a collection of hook functions.
Hook struct {
loadHooks []symbol.LoadHook
unloadHooks []symbol.UnloadHook
mu sync.RWMutex
}
)
// Hook is a collection of hook functions for loading and unloading nodes.
type Hook struct {
loadHooks []symbol.LoadHook
unloadHooks []symbol.UnloadHook
mu sync.RWMutex
}

var _ symbol.LoadHook = &Hook{}
var _ symbol.UnloadHook = &Hook{}

// New returns a new Hooks.
// New creates a new Hook instance.
func New() *Hook {
return &Hook{}
}

// AddLoadHook adds a LoadHook.
// AddLoadHook adds a LoadHook function to the Hook.
func (h *Hook) AddLoadHook(hook symbol.LoadHook) {
h.mu.Lock()
defer h.mu.Unlock()

h.loadHooks = append(h.loadHooks, hook)
}

// AddUnloadHook adds a UnloadHook.
// AddUnloadHook adds an UnloadHook function to the Hook.
func (h *Hook) AddUnloadHook(hook symbol.UnloadHook) {
h.mu.Lock()
defer h.mu.Unlock()

h.unloadHooks = append(h.unloadHooks, hook)
}

// Load runs LoadHooks.
// Load executes LoadHooks on the provided node.
func (h *Hook) Load(n node.Node) error {
h.mu.RLock()
defer h.mu.RUnlock()
Expand All @@ -53,7 +51,7 @@ func (h *Hook) Load(n node.Node) error {
return nil
}

// Unload runs UnloadHooks.
// Unload executes UnloadHooks on the provided node.
func (h *Hook) Unload(n node.Node) error {
h.mu.RLock()
defer h.mu.RUnlock()
Expand Down

0 comments on commit d750912

Please sign in to comment.