From 60014e881335b46febc6a332ed00a12e6c246de9 Mon Sep 17 00:00:00 2001 From: facundoolano Date: Mon, 4 Mar 2024 10:33:55 -0300 Subject: [PATCH] more serve cmd and post updates --- commands/serve.go | 33 +++++++------ .../blog/a-site-server-with-live-reload.org | 49 ++++++++++++++++--- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/commands/serve.go b/commands/serve.go index fe24c6a..06cb7c1 100644 --- a/commands/serve.go +++ b/commands/serve.go @@ -88,7 +88,6 @@ func runWatcher(config *config.Config) (*fsnotify.Watcher, *EventBroker, error) if err != nil { return nil, nil, err } - defer watchProjectFiles(watcher, config) broker := newEventBroker() @@ -119,21 +118,8 @@ func runWatcher(config *config.Config) (*fsnotify.Watcher, *EventBroker, error) return watcher, broker, err } -// Configure the given watcher to notify for changes in the project source files -func watchProjectFiles(watcher *fsnotify.Watcher, config *config.Config) error { - watcher.Add(config.LayoutsDir) - watcher.Add(config.DataDir) - watcher.Add(config.IncludesDir) - // fsnotify watches all files within a dir, but non recursively - // this walks through the src dir and adds watches for each found directory - return filepath.WalkDir(config.SrcDir, func(path string, entry fs.DirEntry, err error) error { - if entry.IsDir() { - watcher.Add(path) - } - return nil - }) -} - +// React to source file change events by re-watching the source directories, +// rebuilding the site and publishing a rebuild event to clients. func rebuildSite(config *config.Config, watcher *fsnotify.Watcher, broker *EventBroker) { fmt.Printf("building site\n") @@ -153,6 +139,21 @@ func rebuildSite(config *config.Config, watcher *fsnotify.Watcher, broker *Event fmt.Println("done\nserving at", config.SiteUrl) } +// Configure the given watcher to notify for changes in the project source files +func watchProjectFiles(watcher *fsnotify.Watcher, config *config.Config) error { + watcher.Add(config.LayoutsDir) + watcher.Add(config.DataDir) + watcher.Add(config.IncludesDir) + // fsnotify watches all files within a dir, but non recursively + // this walks through the src dir and adds watches for each found directory + return filepath.WalkDir(config.SrcDir, func(path string, entry fs.DirEntry, err error) error { + if entry.IsDir() { + watcher.Add(path) + } + return nil + }) +} + // Tweaks the http file system to construct a server that hides the .html suffix from requests. // Based on https://stackoverflow.com/a/57281956/993769 type HTMLFileSystem struct { diff --git a/docs/src/blog/a-site-server-with-live-reload.org b/docs/src/blog/a-site-server-with-live-reload.org index e9325b6..9540837 100644 --- a/docs/src/blog/a-site-server-with-live-reload.org +++ b/docs/src/blog/a-site-server-with-live-reload.org @@ -81,7 +81,6 @@ func runWatcher(config *config.Config) { // new src directories could be triggering this event // so project files need to be re-added every time watchProjectFiles(watcher, config) - site.Build(*config) } }() @@ -101,6 +100,35 @@ func watchProjectFiles(watcher *fsnotify.Watcher, config *config.Config) { } #+end_src +- delay to prevent bursts + +#+begin_src diff +func runWatcher(config *config.Config) { + watcher, _ := fsnotify.NewWatcher() +- defer watchProjectFiles(watcher, config) ++ ++ rebuildAfter := time.AfterFunc(0, func() { ++ watchProjectFiles(watcher, config) ++ site.Build(*config) ++ }) + + go func() { + for event := range watcher.Events { + fmt.Printf("\nfile %s changed, rebuilding site\n", event.Name) + +- watchProjectFiles(watcher, config) +- site.Build(*config) ++ // Schedule a rebuild to trigger after a delay. ++ // If there was another one pending it will be canceled. ++ rebuildAfter.Stop() ++ rebuildAfter.Reset(100 * time.Millisecond) + + + } + }() +} +#+end_src + *** Build optimizations - optimization: worker pool @@ -173,6 +201,7 @@ func (site *site) build() error { } #+end_src +- in other languages, a similar change would have required adding async/await statements on half of my codebase. - optimization: ln static files *** Live reload @@ -250,23 +279,27 @@ newSSE(); -func runWatcher(config *config.Config) { +func runWatcher(config *config.Config) *EventBroker { watcher, _ := fsnotify.NewWatcher() - defer watchProjectFiles(watcher, config) + broker := newEventBroker() + rebuildAfter := time.AfterFunc(0, func() { + watchProjectFiles(watcher, config) + site.Build(*config) ++ broker.publish("rebuild") + }) + + go func() { for event := range watcher.Events { fmt.Printf("\nfile %s changed, rebuilding site\n", event.Name) - // new src directories could be triggering this event - // so project files need to be re-added every time - watchProjectFiles(watcher, config) + // Schedule a rebuild to trigger after a delay. + // If there was another one pending it will be canceled. + rebuildAfter.Stop() + rebuildAfter.Reset(100 * time.Millisecond) - site.Build(*config) -+ broker.publish("rebuild") } }() + return broker } #+end_src -- delay to prevent bursts