1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-27 00:20:27 +02:00

added Context JSVM bind

This commit is contained in:
Gani Georgiev
2024-10-03 13:27:08 +03:00
parent f9d6549469
commit 9797e6c48f
4 changed files with 3144 additions and 3045 deletions

View File

@@ -357,6 +357,27 @@ func baseBinds(vm *goja.Runtime) {
return json.Unmarshal(raw, &dst)
})
vm.Set("Context", func(call goja.ConstructorCall) *goja.Object {
var instance context.Context
oldCtx, ok := call.Argument(0).Export().(context.Context)
if ok {
instance = oldCtx
} else {
instance = context.Background()
}
key := call.Argument(1).Export()
if key != nil {
instance = context.WithValue(instance, key, call.Argument(2).Export())
}
instanceValue := vm.ToValue(instance).(*goja.Object)
instanceValue.SetPrototype(call.This.Prototype())
return instanceValue
})
vm.Set("DynamicModel", func(call goja.ConstructorCall) *goja.Object {
shape, ok := call.Argument(0).Export().(map[string]any)
if !ok || len(shape) == 0 {