added jsvm subscriptions.Message binding

This commit is contained in:
Gani Georgiev
2023-10-07 16:11:31 +03:00
parent 49e3f4ad93
commit e2f806d8bb
6 changed files with 2729 additions and 2646 deletions
+6
View File
@@ -35,6 +35,7 @@ import (
"github.com/pocketbase/pocketbase/tools/mailer"
"github.com/pocketbase/pocketbase/tools/rest"
"github.com/pocketbase/pocketbase/tools/security"
"github.com/pocketbase/pocketbase/tools/subscriptions"
"github.com/pocketbase/pocketbase/tools/types"
"github.com/spf13/cobra"
)
@@ -417,6 +418,11 @@ func baseBinds(vm *goja.Runtime) {
instance := &http.Cookie{}
return structConstructor(vm, call, instance)
})
vm.Set("SubscriptionMessage", func(call goja.ConstructorCall) *goja.Object {
instance := &subscriptions.Message{}
return structConstructor(vm, call, instance)
})
}
func dbxBinds(vm *goja.Runtime) {
+32 -1
View File
@@ -46,7 +46,7 @@ func TestBaseBindsCount(t *testing.T) {
vm := goja.New()
baseBinds(vm)
testBindsCount(vm, "this", 15, t)
testBindsCount(vm, "this", 16, t)
}
func TestBaseBindsReaderToString(t *testing.T) {
@@ -101,6 +101,37 @@ func TestBaseBindsCookie(t *testing.T) {
}
}
func TestBaseBindsSubscriptionMessage(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
vm := goja.New()
baseBinds(vm)
vm.Set("bytesToString", func(b []byte) string {
return string(b)
})
_, err := vm.RunString(`
const payload = {
name: "test",
data: '{"test":123}'
}
const result = new SubscriptionMessage(payload);
if (result.name != payload.name) {
throw new("Expected name " + payload.name + ", got " + result.name);
}
if (bytesToString(result.data) != payload.data) {
throw new("Expected data '" + payload.data + "', got '" + bytesToString(result.data) + "'");
}
`)
if err != nil {
t.Fatal(err)
}
}
func TestBaseBindsRecord(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
File diff suppressed because it is too large Load Diff
+21
View File
@@ -480,6 +480,27 @@ declare class Cookie implements http.Cookie {
constructor(options?: Partial<http.Cookie>)
}
interface SubscriptionMessage extends subscriptions.Message{} // merge
/**
* SubscriptionMessage defines a realtime subscription payload.
*
* Example:
*
* ` + "```" + `js
* onRealtimeConnectRequest((e) => {
* e.client.send(new SubscriptionMessage({
* name: "example",
* data: '{"greeting": "Hello world"}'
* }))
* })
* ` + "```" + `
*
* @group PocketBase
*/
declare class SubscriptionMessage implements subscriptions.Message {
constructor(options?: Partial<subscriptions.Message>)
}
// -------------------------------------------------------------------
// dbxBinds
// -------------------------------------------------------------------