-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: expose load and unload listener
- Loading branch information
1 parent
6b36b04
commit b68c8aa
Showing
5 changed files
with
66 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |