Skip to content

Commit

Permalink
refactor: expose load and unload listener
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 20, 2024
1 parent 6b36b04 commit b68c8aa
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 89 deletions.
4 changes: 2 additions & 2 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func New(config Config) *Runtime {
config.ChartStore = chart.NewStore()
}

config.Hook.AddLoadHook(symbol.NewClusterLoadHook(config.Hook))
config.Hook.AddUnloadHook(symbol.NewClusterUnloadHook(config.Hook))
config.Hook.AddLoadHook(symbol.LoadListenerHook(config.Hook))
config.Hook.AddUnloadHook(symbol.UnloadListenerHook(config.Hook))

symbolTable := symbol.NewTable(symbol.TableOption{
LoadHooks: []symbol.LoadHook{config.Hook},
Expand Down
22 changes: 2 additions & 20 deletions pkg/symbol/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,8 @@ type Cluster struct {
}

var _ node.Node = (*Cluster)(nil)

// NewClusterLoadHook creates a LoadHook for Cluster nodes.
func NewClusterLoadHook(hook LoadHook) LoadHook {
return LoadFunc(func(sb *Symbol) error {
if cluster, ok := node.Unwrap(sb).(*Cluster); ok {
return cluster.Load(hook)
}
return nil
})
}

// NewClusterUnloadHook creates an UnloadHook for Cluster nodes.
func NewClusterUnloadHook(hook UnloadHook) UnloadHook {
return UnloadFunc(func(sb *Symbol) error {
if cluster, ok := node.Unwrap(sb).(*Cluster); ok {
return cluster.Unload(hook)
}
return nil
})
}
var _ LoadListener = (*Cluster)(nil)
var _ UnloadListener = (*Cluster)(nil)

// NewCluster creates a new Cluster with the provided symbol table.
func NewCluster(symbols []*Symbol) *Cluster {
Expand Down
40 changes: 0 additions & 40 deletions pkg/symbol/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,6 @@ import (
"time"
)

func TestNewClusterLoadHook(t *testing.T) {
h := NewClusterLoadHook(LoadFunc(func(_ *Symbol) error {
return nil
}))

n := NewCluster(nil)
defer n.Close()

sb := &Symbol{
Spec: &spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: faker.Word(),
},
Node: n,
}

err := h.Load(sb)
assert.NoError(t, err)
}

func TestNewClusterUnloadHook(t *testing.T) {
h := NewClusterUnloadHook(UnloadFunc(func(_ *Symbol) error {
return nil
}))

n := NewCluster(nil)
defer n.Close()

sb := &Symbol{
Spec: &spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: faker.Word(),
},
Node: n,
}

err := h.Unload(sb)
assert.NoError(t, err)
}

func TestNewCluster(t *testing.T) {
n := NewCluster(nil)
assert.NotNil(t, n)
Expand Down
44 changes: 31 additions & 13 deletions pkg/symbol/loadhook.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
package symbol

// LoadHook defines an interface for handling events when a symbol is loaded.
import "github.com/siyul-park/uniflow/pkg/node"

// LoadHook handles symbol load events.
type LoadHook interface {
// Load is called to handle the loading of a symbol and may return an error.
Load(*Symbol) error
}

// LoadHooks is a slice of LoadHook interfaces, processed sequentially.
// LoadHooks is a collection of LoadHook instances.
type LoadHooks []LoadHook

// LoadListener registers and invokes LoadHook instances.
type LoadListener interface {
Load(LoadHook) error
}

type loadHook struct {
load func(*Symbol) error
fn func(*Symbol) error
}

var _ LoadHook = (LoadHooks)(nil)
var _ LoadHook = (*loadHook)(nil)
var _ LoadHook = (LoadHooks)(nil)

// LoadFunc wraps a function as a LoadHook.
func LoadFunc(fn func(*Symbol) error) LoadHook {
return &loadHook{fn: fn}
}

// LoadFunc creates a new LoadHook from the provided function.
func LoadFunc(load func(*Symbol) error) LoadHook {
return &loadHook{load: load}
// LoadListenerHook creates a LoadHook for nodes implementing LoadListener.
func LoadListenerHook(hook LoadHook) LoadHook {
return LoadFunc(func(symbol *Symbol) error {
if listener, ok := node.Unwrap(symbol).(LoadListener); ok {
return listener.Load(hook)
}
return nil
})
}

func (h LoadHooks) Load(sb *Symbol) error {
for _, hook := range h {
if err := hook.Load(sb); err != nil {
// Load executes all LoadHooks sequentially.
func (hooks LoadHooks) Load(symbol *Symbol) error {
for _, hook := range hooks {
if err := hook.Load(symbol); err != nil {
return err
}
}
return nil
}

func (h *loadHook) Load(sb *Symbol) error {
return h.load(sb)
// Load executes the associated function.
func (h *loadHook) Load(symbol *Symbol) error {
return h.fn(symbol)
}
45 changes: 31 additions & 14 deletions pkg/symbol/unloadhook.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
package symbol

// UnloadHook defines an interface for handling events when a symbol is unloaded.
import "github.com/siyul-park/uniflow/pkg/node"

// UnloadHook handles symbol unload events.
type UnloadHook interface {
// Unload is called when a symbol is unloaded and may return an error.
Unload(*Symbol) error
}

// UnloadHooks is a slice of UnloadHook interfaces, processed in reverse order.
// UnloadHooks is a collection of UnloadHook instances, processed in reverse order.
type UnloadHooks []UnloadHook

// UnloadListener registers and invokes UnloadHook instances.
type UnloadListener interface {
Unload(UnloadHook) error
}

type unloadHook struct {
unload func(*Symbol) error
fn func(*Symbol) error
}

var _ UnloadHook = (UnloadHooks)(nil)
var _ UnloadHook = (*unloadHook)(nil)
var _ UnloadHook = (UnloadHooks)(nil)

// UnloadFunc wraps a function as an UnloadHook.
func UnloadFunc(fn func(*Symbol) error) UnloadHook {
return &unloadHook{fn: fn}
}

// UnloadFunc creates a new UnloadHook from the provided function.
func UnloadFunc(unload func(*Symbol) error) UnloadHook {
return &unloadHook{unload: unload}
// UnloadListenerHook creates an UnloadHook for nodes implementing UnloadListener.
func UnloadListenerHook(hook UnloadHook) UnloadHook {
return UnloadFunc(func(symbol *Symbol) error {
if listener, ok := node.Unwrap(symbol).(UnloadListener); ok {
return listener.Unload(hook)
}
return nil
})
}

func (h UnloadHooks) Unload(sb *Symbol) error {
for i := len(h) - 1; i >= 0; i-- {
hook := h[i]
if err := hook.Unload(sb); err != nil {
// Unload executes all UnloadHooks in reverse order.
func (hooks UnloadHooks) Unload(symbol *Symbol) error {
for i := len(hooks) - 1; i >= 0; i-- {
if err := hooks[i].Unload(symbol); err != nil {
return err
}
}
return nil
}

func (h *unloadHook) Unload(sb *Symbol) error {
return h.unload(sb)
// Unload executes the associated function.
func (h *unloadHook) Unload(symbol *Symbol) error {
return h.fn(symbol)
}

0 comments on commit b68c8aa

Please sign in to comment.