updated cron jsvm bindings and generated types

This commit is contained in:
Gani Georgiev
2023-07-16 23:24:10 +03:00
parent 6179864828
commit 2d1ad16b4f
10 changed files with 8503 additions and 8847 deletions
+16 -12
View File
@@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
@@ -104,12 +103,12 @@ func hooksBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
}
func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
jobs := cron.New()
scheduler := cron.New()
loader.Set("cronAdd", func(jobId, cronExpr, handler string) {
pr := goja.MustCompile("", "{("+handler+").apply(undefined)}", true)
err := jobs.Add(jobId, cronExpr, func() {
err := scheduler.Add(jobId, cronExpr, func() {
executors.run(func(executor *goja.Runtime) error {
_, err := executor.RunProgram(pr)
return err
@@ -120,19 +119,28 @@ func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
}
// start the ticker (if not already)
if jobs.Total() > 0 && !jobs.HasStarted() {
jobs.Start()
if app.IsBootstrapped() && scheduler.Total() > 0 && !scheduler.HasStarted() {
scheduler.Start()
}
})
loader.Set("cronRemove", func(jobId string) {
jobs.Remove(jobId)
scheduler.Remove(jobId)
// stop the ticker if there are no other jobs
if jobs.Total() == 0 {
jobs.Stop()
if scheduler.Total() == 0 {
scheduler.Stop()
}
})
app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
// start the ticker (if not already)
if scheduler.Total() > 0 && !scheduler.HasStarted() {
scheduler.Start()
}
return nil
})
}
func routerBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
@@ -534,10 +542,6 @@ func httpClientBinds(vm *goja.Runtime) {
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 400 {
return nil, fmt.Errorf("request failed with status %d", res.StatusCode)
}
bodyRaw, _ := io.ReadAll(res.Body)
result := &sendResult{
File diff suppressed because it is too large Load Diff
@@ -29,7 +29,7 @@ const heading = `
*
* ` + "```" + `js
* // prints "Hello world!" on every 30 minutes
* cronAdd("hello", "*/30 * * * *", (c) => {
* cronAdd("hello", "*\/30 * * * *", (c) => {
* console.log("Hello world!")
* })
* ` + "```" + `
@@ -45,7 +45,7 @@ declare function cronAdd(
): void;
/**
* CronRemove removes previously registerd cron job by its name.
* CronRemove removes a single registered cron job by its name.
*
* Example:
*
@@ -162,7 +162,7 @@ declare var $app: appWithoutHooks
* ` + "```" + `js
* const records = arrayOf(new Record)
*
* $app.dao().recordQuery(collection).limit(10).all(records)
* $app.dao().recordQuery("articles").limit(10).all(records)
* ` + "```" + `
*
* @group PocketBase
@@ -700,13 +700,30 @@ declare namespace $apis {
// httpClientBinds
// -------------------------------------------------------------------
/**
* ` + "`" + `$http` + "`" + ` defines common methods for working with HTTP requests.
*
* @group PocketBase
*/
declare namespace $http {
/**
* Sends a single HTTP request (_currently only json and plain text requests_).
* Sends a single HTTP request.
*
* @group PocketBase
* Example:
*
* ` + "```" + `js
* const res = $http.send({
* url: "https://example.com",
* data: {"title": "test"}
* method: "post",
* })
*
* console.log(res.statusCode)
* console.log(res.raw)
* console.log(res.json)
* ` + "```" + `
*/
function send(params: {
function send(config: {
url: string,
method?: string, // default to "GET"
data?: { [key:string]: any },
+1 -1
View File
@@ -25,7 +25,7 @@ import (
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/plugins/jsvm/internal/docs/generated"
"github.com/pocketbase/pocketbase/plugins/jsvm/internal/types/generated"
)
const (