From 64d7ab22f34a0fa12fd47b6ca29e40d07cebdc0f Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 14 Jul 2023 16:50:35 +0300 Subject: [PATCH] treat returned false bool from a jsvm hook as hook.stopPropagation --- plugins/jsvm/binds.go | 21 +++++++-------------- plugins/jsvm/binds_test.go | 4 ++-- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/plugins/jsvm/binds.go b/plugins/jsvm/binds.go index 59dfe89d..fbb979e7 100644 --- a/plugins/jsvm/binds.go +++ b/plugins/jsvm/binds.go @@ -76,22 +76,17 @@ func hooksBinds(app core.App, loader *goja.Runtime, executors *vmsPool) { res, err := executor.RunProgram(pr) executor.Set("__args", goja.Undefined()) - // check for returned hook.StopPropagation + // check for returned error or false if res != nil { exported := res.Export() if exported != nil { - if v, ok := exported.(error); ok { + switch v := exported.(type) { + case error: return v - } - } - } - - // check for throwed hook.StopPropagation - if err != nil { - if exception, ok := err.(*goja.Exception); ok { - v, ok := exception.Value().Export().(error) - if ok && errors.Is(v, hook.StopPropagation) { - return hook.StopPropagation + case bool: + if !v { + return hook.StopPropagation + } } } } @@ -344,8 +339,6 @@ func baseBinds(vm *goja.Runtime) { return instanceValue }) - - vm.Set("$stopPropagation", hook.StopPropagation) } func dbxBinds(vm *goja.Runtime) { diff --git a/plugins/jsvm/binds_test.go b/plugins/jsvm/binds_test.go index aa9771c4..af8a147b 100644 --- a/plugins/jsvm/binds_test.go +++ b/plugins/jsvm/binds_test.go @@ -45,7 +45,7 @@ func TestBaseBindsCount(t *testing.T) { vm := goja.New() baseBinds(vm) - testBindsCount(vm, "this", 15, t) + testBindsCount(vm, "this", 14, t) } func TestBaseBindsRecord(t *testing.T) { @@ -1048,7 +1048,7 @@ func TestHooksBinds(t *testing.T) { }, "demo2") onModelBeforeUpdate((e) => { - return $stopPropagation + return false }, "demo2") onModelBeforeUpdate((e) => {