fix(app): use event loop runtime for every operations
All checks were successful
arcad/edge/pipeline/head This commit looks good

This commit is contained in:
2023-04-24 12:16:30 +02:00
parent abc60b9ae3
commit ba9ae6e391
9 changed files with 137 additions and 81 deletions

View File

@ -39,3 +39,14 @@ func NewPromiseProxy(promise *goja.Promise, resolve func(result interface{}), re
return proxy
}
func NewPromiseProxyFrom(rt *goja.Runtime) *PromiseProxy {
promise, resolve, reject := rt.NewPromise()
return NewPromiseProxy(promise, resolve, reject)
}
func IsPromise(v goja.Value) (*goja.Promise, bool) {
promise, ok := v.Export().(*goja.Promise)
return promise, ok
}

View File

@ -17,15 +17,22 @@ var (
)
type Server struct {
runtime *goja.Runtime
loop *eventloop.EventLoop
modules []ServerModule
loop *eventloop.EventLoop
factories []ServerModuleFactory
modules []ServerModule
}
func (s *Server) Load(name string, src string) error {
_, err := s.runtime.RunScript(name, src)
var err error
s.loop.RunOnLoop(func(rt *goja.Runtime) {
_, err = rt.RunScript(name, src)
if err != nil {
err = errors.Wrap(err, "could not run js script")
}
})
if err != nil {
return errors.Wrap(err, "could not run js script")
return errors.WithStack(err)
}
return nil
@ -34,15 +41,15 @@ func (s *Server) Load(name string, src string) error {
func (s *Server) ExecFuncByName(ctx context.Context, funcName string, args ...interface{}) (goja.Value, error) {
ctx = logger.With(ctx, logger.F("function", funcName), logger.F("args", args))
callable, ok := goja.AssertFunction(s.runtime.Get(funcName))
if !ok {
return nil, errors.WithStack(ErrFuncDoesNotExist)
ret, err := s.Exec(ctx, funcName, args...)
if err != nil {
return nil, errors.WithStack(err)
}
return s.Exec(ctx, callable, args...)
return ret, nil
}
func (s *Server) Exec(ctx context.Context, callable goja.Callable, args ...interface{}) (goja.Value, error) {
func (s *Server) Exec(ctx context.Context, callableOrFuncname any, args ...interface{}) (goja.Value, error) {
var (
wg sync.WaitGroup
value goja.Value
@ -51,7 +58,28 @@ func (s *Server) Exec(ctx context.Context, callable goja.Callable, args ...inter
wg.Add(1)
s.loop.RunOnLoop(func(vm *goja.Runtime) {
s.loop.RunOnLoop(func(rt *goja.Runtime) {
var callable goja.Callable
switch typ := callableOrFuncname.(type) {
case goja.Callable:
callable = typ
case string:
call, ok := goja.AssertFunction(rt.Get(typ))
if !ok {
err = errors.WithStack(ErrFuncDoesNotExist)
return
}
callable = call
default:
err = errors.Errorf("callableOrFuncname: expected callable or function name, got '%T'", callableOrFuncname)
return
}
logger.Debug(ctx, "executing callable")
defer wg.Done()
@ -73,7 +101,7 @@ func (s *Server) Exec(ctx context.Context, callable goja.Callable, args ...inter
jsArgs := make([]goja.Value, 0, len(args))
for _, a := range args {
jsArgs = append(jsArgs, vm.ToValue(a))
jsArgs = append(jsArgs, rt.ToValue(a))
}
value, err = callable(nil, jsArgs...)
@ -84,12 +112,11 @@ func (s *Server) Exec(ctx context.Context, callable goja.Callable, args ...inter
wg.Wait()
return value, err
}
if err != nil {
return nil, errors.WithStack(err)
}
func (s *Server) IsPromise(v goja.Value) (*goja.Promise, bool) {
promise, ok := v.Export().(*goja.Promise)
return promise, ok
return value, nil
}
func (s *Server) WaitForPromise(promise *goja.Promise) goja.Value {
@ -135,28 +162,21 @@ func (s *Server) WaitForPromise(promise *goja.Promise) goja.Value {
return value
}
func (s *Server) NewPromise() *PromiseProxy {
promise, resolve, reject := s.runtime.NewPromise()
return NewPromiseProxy(promise, resolve, reject)
}
func (s *Server) ToValue(v interface{}) goja.Value {
return s.runtime.ToValue(v)
}
func (s *Server) Start() error {
s.loop.Start()
for _, mod := range s.modules {
initMod, ok := mod.(InitializableModule)
if !ok {
continue
}
var err error
if err := initMod.OnInit(); err != nil {
return errors.WithStack(err)
s.loop.RunOnLoop(func(rt *goja.Runtime) {
rt.SetFieldNameMapper(goja.TagFieldNameMapper("goja", true))
rt.SetRandSource(createRandomSource())
if err = s.initModules(rt); err != nil {
err = errors.WithStack(err)
}
})
if err != nil {
return errors.WithStack(err)
}
return nil
@ -166,36 +186,46 @@ func (s *Server) Stop() {
s.loop.Stop()
}
func (s *Server) initModules(factories ...ServerModuleFactory) {
runtime := goja.New()
func (s *Server) initModules(rt *goja.Runtime) error {
modules := make([]ServerModule, 0, len(s.factories))
runtime.SetFieldNameMapper(goja.TagFieldNameMapper("goja", true))
runtime.SetRandSource(createRandomSource())
modules := make([]ServerModule, 0, len(factories))
for _, moduleFactory := range factories {
for _, moduleFactory := range s.factories {
mod := moduleFactory(s)
export := runtime.NewObject()
export := rt.NewObject()
mod.Export(export)
runtime.Set(mod.Name(), export)
rt.Set(mod.Name(), export)
modules = append(modules, mod)
}
s.runtime = runtime
for _, mod := range modules {
initMod, ok := mod.(InitializableModule)
if !ok {
continue
}
logger.Debug(context.Background(), "initializing module", logger.F("module", initMod.Name()))
if err := initMod.OnInit(rt); err != nil {
return errors.WithStack(err)
}
}
s.modules = modules
return nil
}
func NewServer(factories ...ServerModuleFactory) *Server {
server := &Server{
factories: factories,
loop: eventloop.NewEventLoop(
eventloop.EnableConsole(false),
),
}
server.initModules(factories...)
return server
}

View File

@ -13,5 +13,5 @@ type ServerModule interface {
type InitializableModule interface {
ServerModule
OnInit() error
OnInit(rt *goja.Runtime) error
}