possibly first working draft

This commit is contained in:
2026-02-01 16:55:40 +01:00
parent e27aadc603
commit 92a6f84540
18 changed files with 450 additions and 270 deletions

View File

@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"
"time"
// "donniemarko/internal/note"
)
type ChangeType int
@ -32,13 +33,21 @@ type Change struct {
type ScannerService struct {
RootDir string
interval time.Duration
lastStates map[string]time.Time
Interval time.Duration
LastStates map[string]time.Time
handler ChangeHandler
}
func NewScanner(path string) *ScannerService {
return &ScannerService{RootDir: path}
return &ScannerService{
RootDir: path,
Interval: 5 * time.Second,
LastStates: make(map[string]time.Time),
}
}
func (s *ScannerService) SetHandler(handler ChangeHandler) {
s.handler = handler
}
func (s *ScannerService) FindAll() ([]string, error) {
@ -63,6 +72,8 @@ func (s *ScannerService) FindAll() ([]string, error) {
return notePath, err
}
// Scan walks the root folder and update the states of each notes if
// it has changed since the last time a scan occured
func (s *ScannerService) Scan() ([]Change, error) {
var changes []Change
currentStates := make(map[string]time.Time)
@ -81,8 +92,10 @@ func (s *ScannerService) Scan() ([]Change, error) {
currentStates[path] = info.ModTime()
lastMod, existed := s.lastStates[path]
lastMod, existed := s.LastStates[path]
if !existed {
// create the note if it didn't exist yet
s.handler.HandleCreate(path)
changes = append(changes, Change{Type: Created, Path: path, ModTime: lastMod})
} else if info.ModTime().After(lastMod) {
changes = append(changes, Change{Type: Modified, Path: path, ModTime: info.ModTime()})
@ -92,18 +105,20 @@ func (s *ScannerService) Scan() ([]Change, error) {
})
// Check for deletions
for path := range s.lastStates {
for path := range s.LastStates {
if _, exists := currentStates[path]; !exists {
changes = append(changes, Change{Type: Deleted, Path: path})
}
}
s.lastStates = currentStates
s.LastStates = currentStates
return changes, nil
}
// Monitor rescan the root folder at each new tick and handle state
// modification
func (s *ScannerService) Monitor(ctx context.Context) error {
ticker := time.NewTicker(s.interval)
ticker := time.NewTicker(s.Interval)
defer ticker.Stop()
for {