edge/pkg/module/lifecycle.go
William Petit ad49c1718c
All checks were successful
arcad/edge/pipeline/head This commit looks good
arcad/edge/pipeline/pr-master This commit looks good
feat: rewrite bus to prevent deadlocks
2023-11-30 15:02:36 +01:00

56 lines
955 B
Go

package module
import (
"context"
"forge.cadoles.com/arcad/edge/pkg/app"
"github.com/dop251/goja"
"gitlab.com/wpetit/goweb/logger"
)
type LifecycleModule struct{}
func (m *LifecycleModule) Name() string {
return "lifecycle"
}
func (m *LifecycleModule) Export(export *goja.Object) {
}
func (m *LifecycleModule) OnInit(ctx context.Context, rt *goja.Runtime) (err error) {
call, ok := goja.AssertFunction(rt.Get("onInit"))
if !ok {
logger.Warn(ctx, "could not find onInit() function")
return nil
}
defer func() {
recovered := recover()
if recovered == nil {
return
}
recoveredErr, ok := recovered.(error)
if !ok {
panic(recovered)
}
err = recoveredErr
}()
call(nil, rt.ToValue(ctx))
return nil
}
func LifecycleModuleFactory() app.ServerModuleFactory {
return func(server *app.Server) app.ServerModule {
module := &LifecycleModule{}
return module
}
}
var _ app.InitializableModule = &LifecycleModule{}