summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-09-27 02:49:01 +0700
committerShulhan <ms@kilabit.info>2021-09-27 02:49:01 +0700
commitfd4d95b0b0a5b53e16b500ce589b0d45926372a9 (patch)
tree23dc49d2dc367340fd546f71011c2737cec64ad4
parente1f38286eab416a3f00b47e1b4d919a74285094e (diff)
downloadgorankusu-fd4d95b0b0a5b53e16b500ce589b0d45926372a9.tar.xz
all: minimize recompilation on changes on _www
Instead of re-run the commands to recompile and embed for each file changes on _www, we queue changes into channel and run the recompile only if the channel is not empty.
-rw-r--r--trunks.go61
1 files changed, 47 insertions, 14 deletions
diff --git a/trunks.go b/trunks.go
index 767d5ee..3594beb 100644
--- a/trunks.go
+++ b/trunks.go
@@ -522,11 +522,12 @@ func watchDocs() {
// into Go source.
//
func watchWww() {
- logp := "watchWww"
- commands := []string{
- "tsc -p _www/tsconfig.json",
- "go run ./internal/generate-memfs",
- }
+ var (
+ logp = "watchWww"
+ changeq = make(chan struct{}, 64)
+ )
+
+ go recompile(changeq)
dirWatcher := &io.DirWatcher{
Options: memfs.Options{
@@ -545,15 +546,7 @@ func watchWww() {
},
Callback: func(ns *io.NodeState) {
mlog.Outf("--- %s: %s: %d\n", logp, ns.Node.SysPath, ns.State)
-
- for _, cmd := range commands {
- mlog.Outf("%s: %s\n", logp, cmd)
- err := exec.Run(cmd, nil, nil)
- if err != nil {
- mlog.Errf("%s: %s", logp, err)
- return
- }
- }
+ changeq <- struct{}{}
},
}
err := dirWatcher.Start()
@@ -561,3 +554,43 @@ func watchWww() {
mlog.Errf("%s: %s", logp, err)
}
}
+
+func recompile(changeq chan struct{}) {
+ var (
+ logp = "recompile"
+ count int
+ changeTicker = time.NewTicker(3 * time.Second)
+ commands = []string{
+ "tsc -p _www/tsconfig.json",
+ "go run ./internal/generate-memfs",
+ }
+ draining bool
+ )
+ for range changeTicker.C {
+ draining = true
+ for draining {
+ select {
+ case <-changeq:
+ count++
+ default:
+ if count == 0 {
+ // No changes.
+ draining = false
+ } else {
+ // All changes has been drained, execute all
+ // commands.
+ for _, cmd := range commands {
+ mlog.Outf("%s: %s\n", logp, cmd)
+ err := exec.Run(cmd, nil, nil)
+ if err != nil {
+ mlog.Errf("%s: %s", logp, err)
+ return
+ }
+ }
+ count = 0
+ draining = false
+ }
+ }
+ }
+ }
+}