Skip to content

Commit

Permalink
Merge pull request #3 from barelyhuman/feat/fs-poller
Browse files Browse the repository at this point in the history
Feat: FS Polling system
  • Loading branch information
barelyhuman committed Jul 13, 2023
2 parents b866d6d + 9208729 commit ee5f992
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 57 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module github.com/barelyhuman/alvu
go 1.18

require (
github.com/barelyhuman/go v0.2.1
github.com/barelyhuman/go v0.2.2-0.20230713173609-2ee88bb52634
github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9
github.com/fsnotify/fsnotify v1.6.0
github.com/otiai10/copy v1.9.0
github.com/vadv/gopher-lua-libs v0.4.1
github.com/yuin/goldmark v1.5.4
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/aws/aws-sdk-go v1.33.0/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/barelyhuman/go v0.2.1 h1:vbRG/i56+7ttbIQ4WZL5USnL5TspyTUn0R5By8RE3po=
github.com/barelyhuman/go v0.2.1/go.mod h1:hox2iDYZAarjpS7jKQeYIi2F+qMA8KLMtCws++L2sSY=
github.com/barelyhuman/go v0.2.2-0.20230713173609-2ee88bb52634 h1:a53Bc1LuSAB9rGbQkBopsYFJNVTgeoUSgnd0do7PDxw=
github.com/barelyhuman/go v0.2.2-0.20230713173609-2ee88bb52634/go.mod h1:hox2iDYZAarjpS7jKQeYIi2F+qMA8KLMtCws++L2sSY=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand All @@ -27,8 +27,6 @@ github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
Expand Down
93 changes: 42 additions & 51 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"sync"

"github.com/barelyhuman/go/env"
"github.com/barelyhuman/go/poller"
ghttp "github.com/cjoudrey/gluahttp"
"github.com/fsnotify/fsnotify"

"github.com/barelyhuman/go/color"
cp "github.com/otiai10/copy"
Expand Down Expand Up @@ -147,6 +147,7 @@ func main() {
serveFlag = flag.Bool("serve", false, "start a local server")
hardWrapsFlag := flag.Bool("hard-wrap", true, "enable hard wrapping of elements with `<br>`")
portFlag := flag.String("port", "3000", "`PORT` to start the server on")
pollDurationFlag := flag.Int("poll", 350, "Polling duration for file changes in milliseconds")

flag.Parse()

Expand All @@ -168,7 +169,7 @@ func main() {
publicPath: publicPath,
}

watcher := NewWatcher(alvuApp)
watcher := NewWatcher(alvuApp, *pollDurationFlag)

if *serveFlag {
watcher.AddDir(pagesPath)
Expand Down Expand Up @@ -864,17 +865,16 @@ func Contains(collection []string, item string) bool {
// FIXME: redundant compile process for the files
type Watcher struct {
alvu *Alvu
notify *fsnotify.Watcher
poller *poller.Poller
dirs []string
}

func NewWatcher(alvu *Alvu) *Watcher {
func NewWatcher(alvu *Alvu, interval int) *Watcher {
watcher := &Watcher{
alvu: alvu,
alvu: alvu,
poller: poller.NewPollWatcher(interval),
}
notifier, err := fsnotify.NewWatcher()
bail(err)
watcher.notify = notifier

return watcher
}

Expand All @@ -887,8 +887,7 @@ func (w *Watcher) AddDir(dirPath string) {
}

w.dirs = append(w.dirs, dirPath)
err := w.notify.Add(dirPath)
bail(err)
w.poller.Add(dirPath)
}

func (w *Watcher) RebuildAlvu() {
Expand Down Expand Up @@ -920,57 +919,49 @@ func (w *Watcher) RebuildFile(filePath string) {
}

func (w *Watcher) StartWatching() {

go w.poller.StartPoller()
go func() {
for {
select {
case event, ok := <-w.notify.Events:
if !ok {
return
}

case evt := <-w.poller.Events:
onDebug(func() {
debugInfo("Events registered")
})

if event.Op&fsnotify.Write == fsnotify.Write {
onDebug(func() {
debugInfo("File Changed")
})
recompiledText := &color.ColorString{}
recompiledText.Blue(logPrefix).Green("Recompiled!").Reset(" ")

recompiledText := &color.ColorString{}
recompiledText.Blue(logPrefix).Green("Recompiled!").Reset(" ")

_, err := os.Stat(event.Name)
// Do nothing if the file doesn't exit, just continue
if err != nil {
continue
}
_, err := os.Stat(evt.Path)

// If alvu file then just build the file, else
// just rebuilt the whole folder since it could
// be a file from the public folder or the _layout file
if w.alvu.IsAlvuFile(event.Name) {
recompilingText := &color.ColorString{}
recompilingText.Blue(logPrefix).Cyan("Recompiling: ").Gray(event.Name).Reset(" ")
fmt.Println(recompilingText.String())
w.RebuildFile(event.Name)
} else {
recompilingText := &color.ColorString{}
recompilingText.Blue(logPrefix).Cyan("Recompiling: ").Gray("All").Reset(" ")
fmt.Println(recompilingText.String())
w.RebuildAlvu()
}

_clientNotifyReload()
fmt.Println(recompiledText.String())
// Do nothing if the file doesn't exit, just continue
if err != nil {
continue
}
case err, ok := <-w.notify.Errors:
if !ok {
return

// If alvu file then just build the file, else
// just rebuilt the whole folder since it could
// be a file from the public folder or the _layout file
if w.alvu.IsAlvuFile(evt.Path) {
recompilingText := &color.ColorString{}
recompilingText.Blue(logPrefix).Cyan("Recompiling: ").Gray(evt.Path).Reset(" ")
fmt.Println(recompilingText.String())
w.RebuildFile(evt.Path)
} else {
recompilingText := &color.ColorString{}
recompilingText.Blue(logPrefix).Cyan("Recompiling: ").Gray("All").Reset(" ")
fmt.Println(recompilingText.String())
w.RebuildAlvu()
}
fmt.Println("Error happened 😢", err)

_clientNotifyReload()
fmt.Println(recompiledText.String())
continue

case err := <-w.poller.Errors:
// If the poller has an error, just crash,
// digesting polling issues without killing the program would make it complicated
// to handle cleanup of all the kind of files that are being maintained by alvu
bail(err)
}
}
}()
Expand All @@ -982,12 +973,12 @@ func _injectLiveReload(layoutHTML *string) string {
}
return *layoutHTML + `<script>
const socket = new WebSocket("ws://localhost:3000/ws");
// Connection opened
socket.addEventListener("open", (event) => {
socket.send("Hello Server!");
});
// Listen for messages
socket.addEventListener("message", (event) => {
if (event.data == "reload") {
Expand Down

0 comments on commit ee5f992

Please sign in to comment.