-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
50 lines (38 loc) · 1.29 KB
/
watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package watchdir
import (
"context"
"time"
)
const (
// DefaultMaxDepth is the default maximum depth to sweep to, if nothing else is specified.
DefaultMaxDepth = 10
// DefaultPollInterval is the default timeout between polling interations, if nothing else is specified.
DefaultPollInterval = 15 * time.Second
// DefaultWriteStabilityThreshold is the default amount of time since last modification before a file is detected.
DefaultWriteStabilityThreshold = 15 * time.Second
)
type Watcher interface {
// Watch starts watching the directory for changes and calls the handler on each change.
Watch(ctx context.Context, handler Handler) error
// Sweep performs a single sweep of the directory and calls the handler on each change.
Sweep(ctx context.Context, handler Handler) error
}
// EventType defines an operation that took place on the watch directory
type EventType uint8
const (
FileAdded = EventType(1 << 0)
FileRemoved = EventType(1 << 1)
AllEvents = 0b11111111
)
// Event represents a file event
type Event struct {
Type EventType
File string
}
type Handler interface {
WatchEvent(ctx context.Context, event Event) error
}
type HandlerFunc func(ctx context.Context, event Event) error
func (h HandlerFunc) WatchEvent(ctx context.Context, event Event) error {
return h(ctx, event)
}