1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-21 13:35:49 +02:00

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

View File

@ -29,10 +29,10 @@ jobs:
- name: Build Admin dashboard UI - name: Build Admin dashboard UI
run: npm --prefix=./ui ci && npm --prefix=./ui run build run: npm --prefix=./ui ci && npm --prefix=./ui run build
# Similar to the above, the jsvm docs are pregenerated locally # Similar to the above, the jsvm types are pregenerated locally
# but its here to ensure that it wasn't forgotten to be executed. # but its here to ensure that it wasn't forgotten to be executed.
- name: Generate jsvm types - name: Generate jsvm types
run: go run ./plugins/jsvm/internal/docs/docs.go run: go run ./plugins/jsvm/internal/types/types.go
# The prebuilt golangci-lint doesn't support go 1.18+ yet # The prebuilt golangci-lint doesn't support go 1.18+ yet
# https://github.com/golangci/golangci-lint/issues/2649 # https://github.com/golangci/golangci-lint/issues/2649

View File

@ -38,14 +38,14 @@
- **!** Renamed `*Options` to `*Config` for consistency and replaced the unnecessary pointers with their value equivalent to keep the applied configuration defaults isolated within their function calls: - **!** Renamed `*Options` to `*Config` for consistency and replaced the unnecessary pointers with their value equivalent to keep the applied configuration defaults isolated within their function calls:
```go ```go
old: pocketbase.NewWithConfig(config *pocketbase.Config) old: pocketbase.NewWithConfig(config *pocketbase.Config) *pocketbase.PocketBase
new: pocketbase.NewWithConfig(config pocketbase.Config) new: pocketbase.NewWithConfig(config pocketbase.Config) *pocketbase.PocketBase
old: core.NewBaseApp(config *core.BaseAppConfig) old: core.NewBaseApp(config *core.BaseAppConfig) *core.BaseApp
new: core.NewBaseApp(config core.BaseAppConfig) new: core.NewBaseApp(config core.BaseAppConfig) *core.BaseApp
old: apis.Serve(app core.App, options *apis.ServeOptions) old: apis.Serve(app core.App, options *apis.ServeOptions) (*http.Server, error)
new: apis.Serve(app core.App, config apis.ServeConfig) new: apis.Serve(app core.App, config apis.ServeConfig) error
old: jsvm.MustRegisterMigrations(app core.App, options *jsvm.MigrationsOptions) old: jsvm.MustRegisterMigrations(app core.App, options *jsvm.MigrationsOptions)
new: jsvm.MustRegister(app core.App, config jsvm.Config) new: jsvm.MustRegister(app core.App, config jsvm.Config)

View File

@ -4,8 +4,8 @@ lint:
test: test:
go test ./... -v --cover go test ./... -v --cover
jsvmdocs: jstypes:
go run ./plugins/jsvm/internal/docs/docs.go go run ./plugins/jsvm/internal/types/types.go
test-report: test-report:
go test ./... -v --cover -coverprofile=coverage.out go test ./... -v --cover -coverprofile=coverage.out

View File

@ -42,7 +42,7 @@ func main() {
app.RootCmd.PersistentFlags().IntVar( app.RootCmd.PersistentFlags().IntVar(
&hooksPool, &hooksPool,
"hooksPool", "hooksPool",
100, 50,
"the total prewarm goja.Runtime instances for the JS app hooks execution", "the total prewarm goja.Runtime instances for the JS app hooks execution",
) )

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io" "io"
"net/http" "net/http"
"reflect" "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) { func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
jobs := cron.New() scheduler := cron.New()
loader.Set("cronAdd", func(jobId, cronExpr, handler string) { loader.Set("cronAdd", func(jobId, cronExpr, handler string) {
pr := goja.MustCompile("", "{("+handler+").apply(undefined)}", true) 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 { executors.run(func(executor *goja.Runtime) error {
_, err := executor.RunProgram(pr) _, err := executor.RunProgram(pr)
return err return err
@ -120,19 +119,28 @@ func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
} }
// start the ticker (if not already) // start the ticker (if not already)
if jobs.Total() > 0 && !jobs.HasStarted() { if app.IsBootstrapped() && scheduler.Total() > 0 && !scheduler.HasStarted() {
jobs.Start() scheduler.Start()
} }
}) })
loader.Set("cronRemove", func(jobId string) { loader.Set("cronRemove", func(jobId string) {
jobs.Remove(jobId) scheduler.Remove(jobId)
// stop the ticker if there are no other jobs // stop the ticker if there are no other jobs
if jobs.Total() == 0 { if scheduler.Total() == 0 {
jobs.Stop() 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) { func routerBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
@ -534,10 +542,6 @@ func httpClientBinds(vm *goja.Runtime) {
} }
defer res.Body.Close() 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) bodyRaw, _ := io.ReadAll(res.Body)
result := &sendResult{ result := &sendResult{

View File

@ -29,7 +29,7 @@ const heading = `
* *
* ` + "```" + `js * ` + "```" + `js
* // prints "Hello world!" on every 30 minutes * // prints "Hello world!" on every 30 minutes
* cronAdd("hello", "*/30 * * * *", (c) => { * cronAdd("hello", "*\/30 * * * *", (c) => {
* console.log("Hello world!") * console.log("Hello world!")
* }) * })
* ` + "```" + ` * ` + "```" + `
@ -45,7 +45,7 @@ declare function cronAdd(
): void; ): void;
/** /**
* CronRemove removes previously registerd cron job by its name. * CronRemove removes a single registered cron job by its name.
* *
* Example: * Example:
* *
@ -162,7 +162,7 @@ declare var $app: appWithoutHooks
* ` + "```" + `js * ` + "```" + `js
* const records = arrayOf(new Record) * const records = arrayOf(new Record)
* *
* $app.dao().recordQuery(collection).limit(10).all(records) * $app.dao().recordQuery("articles").limit(10).all(records)
* ` + "```" + ` * ` + "```" + `
* *
* @group PocketBase * @group PocketBase
@ -700,13 +700,30 @@ declare namespace $apis {
// httpClientBinds // httpClientBinds
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/**
* ` + "`" + `$http` + "`" + ` defines common methods for working with HTTP requests.
*
* @group PocketBase
*/
declare namespace $http { 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, url: string,
method?: string, // default to "GET" method?: string, // default to "GET"
data?: { [key:string]: any }, data?: { [key:string]: any },

View File

@ -25,7 +25,7 @@ import (
"github.com/pocketbase/dbx" "github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core" "github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations" 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 ( const (

View File

@ -57,7 +57,6 @@ func (h *Hook[T]) Add(fn Handler[T]) string {
return id return id
} }
// @todo add also to TaggedHook
// Remove removes a single hook handler by its id. // Remove removes a single hook handler by its id.
func (h *Hook[T]) Remove(id string) { func (h *Hook[T]) Remove(id string) {
h.mux.Lock() h.mux.Lock()
@ -71,7 +70,6 @@ func (h *Hook[T]) Remove(id string) {
} }
} }
// @todo add also to TaggedHook
// RemoveAll removes all registered handlers. // RemoveAll removes all registered handlers.
func (h *Hook[T]) RemoveAll() { func (h *Hook[T]) RemoveAll() {
h.mux.Lock() h.mux.Lock()