more serve cmd and post updates

This commit is contained in:
facundoolano 2024-03-04 10:33:55 -03:00
parent ff28ece092
commit 60014e8813
2 changed files with 58 additions and 24 deletions

View file

@ -88,7 +88,6 @@ func runWatcher(config *config.Config) (*fsnotify.Watcher, *EventBroker, error)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
defer watchProjectFiles(watcher, config)
broker := newEventBroker() broker := newEventBroker()
@ -119,21 +118,8 @@ func runWatcher(config *config.Config) (*fsnotify.Watcher, *EventBroker, error)
return watcher, broker, err return watcher, broker, err
} }
// Configure the given watcher to notify for changes in the project source files // React to source file change events by re-watching the source directories,
func watchProjectFiles(watcher *fsnotify.Watcher, config *config.Config) error { // rebuilding the site and publishing a rebuild event to clients.
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
})
}
func rebuildSite(config *config.Config, watcher *fsnotify.Watcher, broker *EventBroker) { func rebuildSite(config *config.Config, watcher *fsnotify.Watcher, broker *EventBroker) {
fmt.Printf("building site\n") 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) 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. // Tweaks the http file system to construct a server that hides the .html suffix from requests.
// Based on https://stackoverflow.com/a/57281956/993769 // Based on https://stackoverflow.com/a/57281956/993769
type HTMLFileSystem struct { type HTMLFileSystem struct {

View file

@ -81,7 +81,6 @@ func runWatcher(config *config.Config) {
// new src directories could be triggering this event // new src directories could be triggering this event
// so project files need to be re-added every time // so project files need to be re-added every time
watchProjectFiles(watcher, config) watchProjectFiles(watcher, config)
site.Build(*config) site.Build(*config)
} }
}() }()
@ -101,6 +100,35 @@ func watchProjectFiles(watcher *fsnotify.Watcher, config *config.Config) {
} }
#+end_src #+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 *** Build optimizations
- optimization: worker pool - optimization: worker pool
@ -173,6 +201,7 @@ func (site *site) build() error {
} }
#+end_src #+end_src
- in other languages, a similar change would have required adding async/await statements on half of my codebase.
- optimization: ln static files - optimization: ln static files
*** Live reload *** Live reload
@ -250,23 +279,27 @@ newSSE();
-func runWatcher(config *config.Config) { -func runWatcher(config *config.Config) {
+func runWatcher(config *config.Config) *EventBroker { +func runWatcher(config *config.Config) *EventBroker {
watcher, _ := fsnotify.NewWatcher() watcher, _ := fsnotify.NewWatcher()
defer watchProjectFiles(watcher, config)
+ broker := newEventBroker() + broker := newEventBroker()
rebuildAfter := time.AfterFunc(0, func() {
watchProjectFiles(watcher, config)
site.Build(*config)
+ broker.publish("rebuild")
})
go func() { go func() {
for event := range watcher.Events { for event := range watcher.Events {
fmt.Printf("\nfile %s changed, rebuilding site\n", event.Name) fmt.Printf("\nfile %s changed, rebuilding site\n", event.Name)
// new src directories could be triggering this event // Schedule a rebuild to trigger after a delay.
// so project files need to be re-added every time // If there was another one pending it will be canceled.
watchProjectFiles(watcher, config) rebuildAfter.Stop()
rebuildAfter.Reset(100 * time.Millisecond)
site.Build(*config)
+ broker.publish("rebuild")
} }
}() }()
+ return broker + return broker
} }
#+end_src #+end_src
- delay to prevent bursts