From f6b83b0933a67aa2cdb43767f5ef6f9b180a6d1e Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 3 Oct 2021 14:03:55 +1100 Subject: [PATCH] [v2] New -debounce flag to configure debounce time of dev server --- v2/cmd/wails/internal/commands/dev/dev.go | 23 +++++++++++++++++-- v2/internal/project/project.go | 3 +++ .../docs/guides/application-development.mdx | 17 ++++++++++---- website/docs/reference/cli.mdx | 3 ++- website/docs/reference/project-config.mdx | 3 ++- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/v2/cmd/wails/internal/commands/dev/dev.go b/v2/cmd/wails/internal/commands/dev/dev.go index f6d432ed8..604fa5b26 100644 --- a/v2/cmd/wails/internal/commands/dev/dev.go +++ b/v2/cmd/wails/internal/commands/dev/dev.go @@ -93,6 +93,9 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { forceBuild := false command.BoolFlag("f", "Force build application", &forceBuild) + debounceMS := 100 + command.IntFlag("debounce", "The amount of time to wait to trigger a reload on change", &debounceMS) + command.Action(func() error { // Create logger @@ -249,10 +252,26 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { } }) + if debounceMS == 100 && projectConfig.DebounceMS != 100 { + if projectConfig.DebounceMS == 0 { + projectConfig.DebounceMS = 100 + } + debounceMS = projectConfig.DebounceMS + } + + if debounceMS != projectConfig.DebounceMS { + projectConfig.DebounceMS = debounceMS + err := projectConfig.Save() + if err != nil { + return err + } + } + + LogGreen("Using reload debounce setting of %d milliseconds", debounceMS) + // Main Loop quit := false - // Use 100ms debounce - interval := 100 * time.Millisecond + interval := time.Duration(debounceMS) * time.Millisecond timer := time.NewTimer(interval) rebuild := false reload := false diff --git a/v2/internal/project/project.go b/v2/internal/project/project.go index f5f567d2e..6fbf1391b 100644 --- a/v2/internal/project/project.go +++ b/v2/internal/project/project.go @@ -46,6 +46,9 @@ type Project struct { // Fully qualified filename filename string + + // The debounce time for hot-reload of the built-in dev server + DebounceMS int `json:"debounceMS"` } func (p *Project) Save() error { diff --git a/website/docs/guides/application-development.mdx b/website/docs/guides/application-development.mdx index aabc02543..46f088597 100644 --- a/website/docs/guides/application-development.mdx +++ b/website/docs/guides/application-development.mdx @@ -161,11 +161,20 @@ The second, if given, will be executed in the `frontend` directory to build the If these 2 keys aren't given, then Wails does absolutely nothing with the frontend. It is only expecting that `embed.FS`. -## Live Reloading +## Built in Dev Server -Running `wails dev` will start a file watcher in your project directory. By default, if any file changes, wails checks -if it was an application file (default: `.go`, configurable with `-e` flag). If it was, then it will rebuild your -application and relaunch it. If the changed file was in the `assetdir` directory, it will issue a reload. +Running `wails dev` will start the built in dev server which will start a file watcher in your project directory. By +default, if any file changes, wails checks if it was an application file (default: `.go`, configurable with `-e` flag). +If it was, then it will rebuild your application and relaunch it. If the changed file was in the `assetdir` directory, +it will issue a reload after a short amount of time. + +The dev server uses a technique called "debouncing" which means it doesn't reload straight away, +as there may be multiple files changed in a short amount of time. When a trigger occurs, it waits for a set amount of time +before issuing a reload. If another trigger happens, it resets to the wait time again. By default this value is `100ms`. +If this value doesn't work for your project, it can be configured using the `-debounce` flag. If used, this value will +be saved to your project config and become the default. + +## External Dev Server Some frameworks come with their own live-reloading server, however they will not be able to take advantage of the Wails Go bindings. In this scenario, it is best to run a watcher script that rebuilds the project into the build directory, which diff --git a/website/docs/reference/cli.mdx b/website/docs/reference/cli.mdx index dc1af0632..349aa8eb5 100644 --- a/website/docs/reference/cli.mdx +++ b/website/docs/reference/cli.mdx @@ -131,7 +131,8 @@ Your system is ready for Wails development! | -loglevel "loglevel"| Loglevel to use - Trace, Debug, Info, Warning, Error | Debug | | -noreload | Disable automatic reload when assets change | | | -v | Verbosity level (0 - silent, 1 - standard, 2 - verbose) | 1 | -| -wailsjsdir | The directory to generate the generated Wails JS modules | Value store in `wails.json` | +| -wailsjsdir | The directory to generate the generated Wails JS modules | Value in `wails.json` | +| -debounce | The time to wait for reload after an asset change is detected | 100 (milliseconds) | If the `-assetdir` or `-wailsjsdir` flags are provided on the command line, they are saved in `wails.json`, and become the defaults for subsequent invocations. diff --git a/website/docs/reference/project-config.mdx b/website/docs/reference/project-config.mdx index addc74d15..e6e9e7aca 100644 --- a/website/docs/reference/project-config.mdx +++ b/website/docs/reference/project-config.mdx @@ -14,7 +14,8 @@ The project config resides in the `wails.json` file in the project directory. Th "frontend:build": "[The command to build the assets, run in the frontend directory - often `npm run build`]", "wailsjsdir": "[Relative path to the directory that the auto-generated JS modules will be created]", "version": "[Project config version]", - "outputfilename": "[The name of the binary]" + "outputfilename": "[The name of the binary]", + "debounceMS": 100, // The default time the dev server waits to reload when it detects a vhange in assets } ```