Skip to content

Commit 6cc7bd4

Browse files
authored
Add work-delay as flag (#95)
Make the internal WorkDelay configurable via flag.
1 parent ffd1abe commit 6cc7bd4

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ the env var `GO111MODULE=on`, which enables you to develop outside of
3737
| | | **file watch** |
3838
|`-polling=…` | false | Use polling instead of FS notifications to detect changes. Default is false
3939
|`-polling-interval=…` | 100 | Milliseconds of interval between polling file changes when polling option is selected
40+
|`-work-delay=…` | 900 | Milliseconds to wait (debounce) before starting a build after file changes settle; each new change resets the timer. Must be a positive integer.|
4041
| | | **misc** |
4142
|`-color=_` | false | Colorize the output of the daemon's status messages. |
4243
|`-log-prefix=_` | true | Prefix all child process output with stdout/stderr labels and log timestamps. |

daemon.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ There are command line options.
4949
FILE WATCH
5050
-polling - Use polling instead of FS notifications to detect changes. Default is false
5151
-polling-interval - Milliseconds of interval between polling file changes when polling option is selected
52+
-work-delay - Milliseconds to wait (debounce) before starting a build after file changes settle (must be > 0)
5253
5354
MISC
5455
-color - Enable colorized output
@@ -82,8 +83,9 @@ import (
8283
"github.com/fatih/color"
8384
)
8485

85-
// Milliseconds to wait for the next job to begin after a file change
86-
const WorkDelay = 900
86+
// DefaultWorkDelay is the default -work-delay value: milliseconds to wait before
87+
// starting a build after file changes (inrush / debounce).
88+
const DefaultWorkDelay = 900
8789

8890
// Default pattern to match files which trigger a build
8991
const FilePattern = `(.+\.go|.+\.c)$`
@@ -135,6 +137,7 @@ var (
135137
flagVerbose = flag.Bool("verbose", false, "Be verbose about which directories are watched.")
136138
flagPolling = flag.Bool("polling", false, "Use polling method to watch file change instead of fsnotify")
137139
flagPollingInterval = flag.Int("polling-interval", 100, "Milliseconds of interval between polling file changes when polling option is selected")
140+
flagWorkDelay = flag.Int("work-delay", DefaultWorkDelay, "Milliseconds to wait (debounce) before starting a build after file changes settle; must be > 0")
138141

139142
// initialized in main() due to custom type.
140143
flagDirectories globList
@@ -205,11 +208,11 @@ func matchesPattern(pattern *regexp.Regexp, file string) bool {
205208
}
206209

207210
// Accept build jobs and start building when there are no jobs rushing in.
208-
// The inrush protection is WorkDelay milliseconds long, in this period
211+
// The inrush protection is -work-delay milliseconds long; in this period
209212
// every incoming job will reset the timer.
210213
func builder(jobs <-chan string, buildStarted chan<- string, buildDone chan<- bool) {
211214
createThreshold := func() <-chan time.Time {
212-
return time.After(time.Duration(WorkDelay * time.Millisecond))
215+
return time.After(time.Duration(*flagWorkDelay) * time.Millisecond)
213216
}
214217

215218
threshold := createThreshold()
@@ -409,6 +412,10 @@ func main() {
409412

410413
flag.Parse()
411414

415+
if *flagWorkDelay <= 0 {
416+
log.Fatal("-work-delay must be a positive integer")
417+
}
418+
412419
if !*flagLogPrefix {
413420
log.SetFlags(0)
414421
}

0 commit comments

Comments
 (0)