Skip to content

Commit

Permalink
Simplified interface; verified functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
joliver committed Jan 7, 2025
1 parent 46e8b43 commit c1c4888
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
14 changes: 4 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package filewatcher

import (
"context"
"strings"
"time"
)

Expand All @@ -16,11 +15,7 @@ func (singleton) Context(value context.Context) option {
return func(this *configuration) { this.context = value }
}
func (singleton) Filenames(values ...string) option {
return func(this *configuration) {
for i := range values {
values[i] = strings.TrimSpace(values[i])
}
}
return func(this *configuration) { this.filenames = values }
}
func (singleton) Interval(value time.Duration) option {
return func(this *configuration) { this.interval = value }
Expand All @@ -44,7 +39,7 @@ func (singleton) defaults(options ...option) []option {
return append([]option{
Options.Context(context.Background()),
Options.Filenames(),
Options.Interval(time.Hour),
Options.Interval(time.Minute),
Options.Notify(func() {}),
Options.Logger(nop{}),
}, options...)
Expand All @@ -71,6 +66,5 @@ type nop struct{}

func (nop) Printf(string, ...any) {}

func (nop) Initialize() error { return nil }
func (nop) Listen() {}
func (nop) Close() error { return nil }
func (nop) Listen() {}
func (nop) Close() error { return nil }
1 change: 0 additions & 1 deletion interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package filewatcher
import "io"

type ListenCloser interface {
Initialize() error
Listen()
io.Closer
}
Expand Down
30 changes: 18 additions & 12 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"os"
"strings"
"time"
)

Expand All @@ -18,13 +19,15 @@ type pollingWatcher struct {
}

func newSimpleWatcher(config configuration) ListenCloser {
if len(config.filenames) == 0 || config.interval < 0 {
return nop{}
}

filenames := make([]string, 0, len(config.filenames))
for _, filename := range config.filenames {
filenames = append(filenames, filename)
if filename = strings.TrimSpace(filename); len(filename) > 0 {
filenames = append(filenames, filename)
}
}

if len(config.filenames) == 0 || config.interval < 0 {
return nop{}
}

child, shutdown := context.WithCancel(config.context)
Expand All @@ -40,15 +43,17 @@ func newSimpleWatcher(config configuration) ListenCloser {
}

func (this *pollingWatcher) Listen() {
this.update()
for i := range this.filenames {
this.known[i] = this.lastModified(this.filenames[i])
}

for this.sleep() {
if this.update() {
this.notify()
}
}
}
func (this *pollingWatcher) update() (updated bool) {
func (this *pollingWatcher) update() bool {
count := 0

for index, _ := range this.filenames {
Expand All @@ -66,13 +71,16 @@ func (this *pollingWatcher) update() (updated bool) {
}

count++
updated = true
this.known[index] = lastModified
this.logger.Printf("[INFO] Watched file [%s] was modified on [%s].", this.filenames[index], lastModified.String())
}

if count == 0 {
return false
}

this.logger.Printf("[INFO] [%d] watched files modified, notifying subscribers...", count)
return updated
return true
}
func (this *pollingWatcher) lastModified(filename string) time.Time {
// simple: using last-modification timestamp instead of file hash
Expand All @@ -84,12 +92,10 @@ func (this *pollingWatcher) sleep() bool {
ctx, cancel := context.WithTimeout(this.childContext, this.interval)
defer cancel()
<-ctx.Done()
return errors.Is(ctx.Err(), context.Canceled)
return errors.Is(ctx.Err(), context.DeadlineExceeded)
}

func (this *pollingWatcher) Close() error {
this.shutdown()
return nil
}

func (this *pollingWatcher) Initialize() error { return nil }

0 comments on commit c1c4888

Please sign in to comment.