Merge pull request #608 from yflau/patch-1

support custom loaders
This commit is contained in:
Max 2024-05-08 06:26:49 +08:00 committed by GitHub
commit 9f7af5e4da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -39,6 +39,18 @@ import (
"github.com/yaoapp/yao/widgets"
)
// LoadHooks used to load custom widgets/processes
var LoadHooks = map[string]func(config.Config) error{}
// RegisterLoadHook register custom load hook
func RegisterLoadHook(name string, hook func(config.Config) error) error {
if _, ok := LoadHooks[name]; ok {
return fmt.Errorf("load hook %s already exists", name)
}
LoadHooks[name] = hook
return nil
}
// LoadOption the load option
type LoadOption struct {
Action string `json:"action"`
@ -225,6 +237,13 @@ func Load(cfg config.Config, options LoadOption) (err error) {
printErr(cfg.Mode, "Pipe", err)
}
for name, hook := range LoadHooks {
err = hook(cfg)
if err != nil {
printErr(cfg.Mode, name, err)
}
}
// Execute AfterLoad Process if exists
if share.App.AfterLoad != "" && !options.IgnoredAfterLoad {
p, err := process.Of(share.App.AfterLoad, options)