mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-03-18 21:57:50 +02:00
added Context JSVM bind
This commit is contained in:
parent
f9d6549469
commit
9797e6c48f
@ -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 {
|
||||
|
@ -43,7 +43,7 @@ func TestBaseBindsCount(t *testing.T) {
|
||||
vm := goja.New()
|
||||
baseBinds(vm)
|
||||
|
||||
testBindsCount(vm, "this", 34, t)
|
||||
testBindsCount(vm, "this", 35, t)
|
||||
}
|
||||
|
||||
func TestBaseBindsSleep(t *testing.T) {
|
||||
@ -138,6 +138,30 @@ func TestBaseBindsUnmarshal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBaseBindsContext(t *testing.T) {
|
||||
vm := goja.New()
|
||||
baseBinds(vm)
|
||||
|
||||
_, err := vm.RunString(`
|
||||
const base = new Context(null, "a", 123);
|
||||
const sub = new Context(base, "b", 456);
|
||||
|
||||
const scenarios = [
|
||||
{key: "a", expected: 123},
|
||||
{key: "b", expected: 456},
|
||||
];
|
||||
|
||||
for (let s of scenarios) {
|
||||
if (sub.value(s.key) != s.expected) {
|
||||
throw new("Expected " +s.key + " value " + s.expected + ", got " + sub.value(s.key));
|
||||
}
|
||||
}
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBaseBindsCookie(t *testing.T) {
|
||||
vm := goja.New()
|
||||
baseBinds(vm)
|
||||
|
6115
plugins/jsvm/internal/types/generated/types.d.ts
vendored
6115
plugins/jsvm/internal/types/generated/types.d.ts
vendored
File diff suppressed because it is too large
Load Diff
@ -248,6 +248,33 @@ declare class DynamicModel {
|
||||
constructor(shape?: { [key:string]: any })
|
||||
}
|
||||
|
||||
interface Context extends context.Context{} // merge
|
||||
/**
|
||||
* Context creates a new empty Go context.Context.
|
||||
*
|
||||
* This is usually used as part of some Go transitive bindings.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ` + "```" + `js
|
||||
* const blank = new Context()
|
||||
*
|
||||
* // with single key-value pair
|
||||
* const base = new Context(null, "a", 123)
|
||||
* console.log(base.value("a")) // 123
|
||||
*
|
||||
* // extend with additional key-value pair
|
||||
* const sub = new Context(base, "b", 456)
|
||||
* console.log(sub.value("a")) // 123
|
||||
* console.log(sub.value("b")) // 456
|
||||
* ` + "```" + `
|
||||
*
|
||||
* @group PocketBase
|
||||
*/
|
||||
declare class Context implements context.Context {
|
||||
constructor(parentCtx?: Context, key?: any, value?: any)
|
||||
}
|
||||
|
||||
/**
|
||||
* Record model class.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user