This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
72 lines (59 loc) · 1.8 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (C) 2019 The CodeActual Go Environment Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package watcher
import "time"
// Op is used for file/directory operation codes.
type Op uint8
const (
Create Op = 1 << iota
Rename
Remove
Write
)
func (o Op) String() string {
switch o {
case Create:
return "Create"
case Rename:
return "Rename"
case Remove:
return "Remove"
default:
return "Write"
}
}
// Event instances are passed to Subscriber implementations on file/directory activity.
type Event struct {
// Path holds the absolute path to the file/directory.
Path string
// Op defines the file/directory operation.
Op Op
}
// Subscriber implementations receive Event and error values.
type Subscriber interface {
Event(Event)
Error(error)
}
type Watcher interface {
// AddSubscriber appends the list of subscribers that receive event/error details.
AddSubscriber(Subscriber) error
// AddPath appends the file/directory (non-recursive) to the watch list and
// begins monitoring in a new goroutine.
//
// Absolute and relative paths are supported. However all paths are made absolute internally.
AddPath(string) error
// RemovePath stops the file/directory (non-recursive) from being watched.
//
// Absolute and relative paths are supported.
RemovePath(string) error
// Close ends all monitoring behavior and clears the watch/subscriber list.
Close() error
// Set the amount of time to wait for duplicate events (same Event.String output value)
// to be received before broadcasting one Event value to subscribers.
//
// Implementations should not debounce events if this method is not called.
Debounce(time.Duration)
}