@eduardo191labs/watcher 0.0.0-stage → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +82 -0
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/binding.gyp +125 -0
- package/index.d.ts +49 -0
- package/index.js +55 -0
- package/index.js.flow +45 -0
- package/package.json +61 -3
- package/scripts/build-from-source.js +16 -0
- package/src/Backend.cc +186 -0
- package/src/Backend.hh +37 -0
- package/src/Debounce.cc +113 -0
- package/src/Debounce.hh +49 -0
- package/src/DirTree.cc +164 -0
- package/src/DirTree.hh +50 -0
- package/src/Event.hh +109 -0
- package/src/Glob.cc +22 -0
- package/src/Glob.hh +34 -0
- package/src/PromiseRunner.hh +101 -0
- package/src/Signal.hh +46 -0
- package/src/Watcher.cc +241 -0
- package/src/Watcher.hh +73 -0
- package/src/binding.cc +268 -0
- package/src/kqueue/KqueueBackend.cc +306 -0
- package/src/kqueue/KqueueBackend.hh +35 -0
- package/src/linux/InotifyBackend.cc +236 -0
- package/src/linux/InotifyBackend.hh +34 -0
- package/src/macos/FSEventsBackend.cc +338 -0
- package/src/macos/FSEventsBackend.hh +20 -0
- package/src/shared/BruteForceBackend.cc +41 -0
- package/src/shared/BruteForceBackend.hh +25 -0
- package/src/unix/fts.cc +50 -0
- package/src/unix/legacy.cc +77 -0
- package/src/wasm/WasmBackend.cc +132 -0
- package/src/wasm/WasmBackend.hh +34 -0
- package/src/wasm/include.h +74 -0
- package/src/watchman/BSER.cc +302 -0
- package/src/watchman/BSER.hh +69 -0
- package/src/watchman/IPC.hh +175 -0
- package/src/watchman/WatchmanBackend.cc +342 -0
- package/src/watchman/WatchmanBackend.hh +35 -0
- package/src/windows/WindowsBackend.cc +282 -0
- package/src/windows/WindowsBackend.hh +18 -0
- package/src/windows/win_utils.cc +44 -0
- package/src/windows/win_utils.hh +11 -0
- package/wrapper.js +88 -0
package/src/Glob.cc
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#include "Glob.hh"
|
|
2
|
+
|
|
3
|
+
#ifdef __wasm32__
|
|
4
|
+
extern "C" bool wasm_regex_match(const char *s, const char *regex);
|
|
5
|
+
#endif
|
|
6
|
+
|
|
7
|
+
Glob::Glob(std::string raw) {
|
|
8
|
+
mRaw = raw;
|
|
9
|
+
mHash = std::hash<std::string>()(raw);
|
|
10
|
+
#ifndef __wasm32__
|
|
11
|
+
mRegex = std::regex(raw);
|
|
12
|
+
#endif
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
bool Glob::isIgnored(std::string relative_path) const {
|
|
16
|
+
// Use native JS regex engine for wasm to reduce binary size.
|
|
17
|
+
#ifdef __wasm32__
|
|
18
|
+
return wasm_regex_match(relative_path.c_str(), mRaw.c_str());
|
|
19
|
+
#else
|
|
20
|
+
return std::regex_match(relative_path, mRegex);
|
|
21
|
+
#endif
|
|
22
|
+
}
|
package/src/Glob.hh
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#ifndef GLOB_H
|
|
2
|
+
#define GLOB_H
|
|
3
|
+
|
|
4
|
+
#include <unordered_set>
|
|
5
|
+
#include <regex>
|
|
6
|
+
|
|
7
|
+
struct Glob {
|
|
8
|
+
std::size_t mHash;
|
|
9
|
+
std::string mRaw;
|
|
10
|
+
#ifndef __wasm32__
|
|
11
|
+
std::regex mRegex;
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
Glob(std::string raw);
|
|
15
|
+
|
|
16
|
+
bool operator==(const Glob &other) const {
|
|
17
|
+
return mHash == other.mHash && mRaw == other.mRaw;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
bool isIgnored(std::string relative_path) const;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
namespace std
|
|
24
|
+
{
|
|
25
|
+
template <>
|
|
26
|
+
struct hash<Glob>
|
|
27
|
+
{
|
|
28
|
+
size_t operator()(const Glob& g) const {
|
|
29
|
+
return g.mHash;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#endif
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
#ifndef PROMISE_RUNNER_H
|
|
2
|
+
#define PROMISE_RUNNER_H
|
|
3
|
+
|
|
4
|
+
#include <node_api.h>
|
|
5
|
+
#include "wasm/include.h"
|
|
6
|
+
#include <napi.h>
|
|
7
|
+
|
|
8
|
+
using namespace Napi;
|
|
9
|
+
|
|
10
|
+
class PromiseRunner {
|
|
11
|
+
public:
|
|
12
|
+
const Env env;
|
|
13
|
+
Promise::Deferred deferred;
|
|
14
|
+
|
|
15
|
+
PromiseRunner(Env env) : env(env), deferred(Promise::Deferred::New(env)) {
|
|
16
|
+
napi_status status = napi_create_async_work(env, nullptr, env.Undefined(),
|
|
17
|
+
onExecute, onWorkComplete, this, &work);
|
|
18
|
+
if (status != napi_ok) {
|
|
19
|
+
work = nullptr;
|
|
20
|
+
const napi_extended_error_info *error_info = 0;
|
|
21
|
+
napi_get_last_error_info(env, &error_info);
|
|
22
|
+
if (error_info->error_message) {
|
|
23
|
+
Error::New(env, error_info->error_message).ThrowAsJavaScriptException();
|
|
24
|
+
} else {
|
|
25
|
+
Error::New(env).ThrowAsJavaScriptException();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
virtual ~PromiseRunner() {}
|
|
31
|
+
|
|
32
|
+
Value queue() {
|
|
33
|
+
if (work) {
|
|
34
|
+
napi_status status = napi_queue_async_work(env, work);
|
|
35
|
+
if (status != napi_ok) {
|
|
36
|
+
onError(Error::New(env));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return deferred.Promise();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private:
|
|
44
|
+
napi_async_work work;
|
|
45
|
+
std::string error;
|
|
46
|
+
|
|
47
|
+
static void onExecute(napi_env env, void *this_pointer) {
|
|
48
|
+
PromiseRunner* self = (PromiseRunner*) this_pointer;
|
|
49
|
+
try {
|
|
50
|
+
self->execute();
|
|
51
|
+
} catch (std::exception &err) {
|
|
52
|
+
self->error = err.what();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static void onWorkComplete(napi_env env, napi_status status, void *this_pointer) {
|
|
57
|
+
PromiseRunner* self = (PromiseRunner*) this_pointer;
|
|
58
|
+
if (status != napi_cancelled) {
|
|
59
|
+
HandleScope scope(self->env);
|
|
60
|
+
if (status == napi_ok) {
|
|
61
|
+
status = napi_delete_async_work(self->env, self->work);
|
|
62
|
+
if (status == napi_ok) {
|
|
63
|
+
if (self->error.size() == 0) {
|
|
64
|
+
self->onOK();
|
|
65
|
+
} else {
|
|
66
|
+
self->onError(Error::New(self->env, self->error));
|
|
67
|
+
}
|
|
68
|
+
delete self;
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// fallthrough for error handling
|
|
75
|
+
const napi_extended_error_info *error_info = 0;
|
|
76
|
+
napi_get_last_error_info(env, &error_info);
|
|
77
|
+
if (error_info->error_message){
|
|
78
|
+
self->onError(Error::New(env, error_info->error_message));
|
|
79
|
+
} else {
|
|
80
|
+
self->onError(Error::New(env));
|
|
81
|
+
}
|
|
82
|
+
delete self;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
virtual void execute() {}
|
|
86
|
+
virtual Value getResult() {
|
|
87
|
+
return env.Null();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
void onOK() {
|
|
91
|
+
HandleScope scope(env);
|
|
92
|
+
Value result = getResult();
|
|
93
|
+
deferred.Resolve(result);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
void onError(const Error &e) {
|
|
97
|
+
deferred.Reject(e.Value());
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
#endif
|
package/src/Signal.hh
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#ifndef SIGNAL_H
|
|
2
|
+
#define SIGNAL_H
|
|
3
|
+
|
|
4
|
+
#include <mutex>
|
|
5
|
+
#include <condition_variable>
|
|
6
|
+
|
|
7
|
+
class Signal {
|
|
8
|
+
public:
|
|
9
|
+
Signal() : mFlag(false), mWaiting(false) {}
|
|
10
|
+
void wait() {
|
|
11
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
12
|
+
while (!mFlag) {
|
|
13
|
+
mWaiting = true;
|
|
14
|
+
mCond.wait(lock);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
std::cv_status waitFor(std::chrono::milliseconds ms) {
|
|
19
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
20
|
+
return mCond.wait_for(lock, ms);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
void notify() {
|
|
24
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
25
|
+
mFlag = true;
|
|
26
|
+
mCond.notify_all();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
void reset() {
|
|
30
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
31
|
+
mFlag = false;
|
|
32
|
+
mWaiting = false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
bool isWaiting() {
|
|
36
|
+
return mWaiting;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private:
|
|
40
|
+
bool mFlag;
|
|
41
|
+
bool mWaiting;
|
|
42
|
+
std::mutex mMutex;
|
|
43
|
+
std::condition_variable mCond;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
#endif
|
package/src/Watcher.cc
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
#include "Watcher.hh"
|
|
2
|
+
#include <unordered_set>
|
|
3
|
+
|
|
4
|
+
using namespace Napi;
|
|
5
|
+
|
|
6
|
+
struct WatcherHash {
|
|
7
|
+
std::size_t operator() (WatcherRef const &k) const {
|
|
8
|
+
return std::hash<std::string>()(k->mDir);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
struct WatcherCompare {
|
|
13
|
+
size_t operator() (WatcherRef const &a, WatcherRef const &b) const {
|
|
14
|
+
return *a == *b;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
static std::unordered_set<WatcherRef , WatcherHash, WatcherCompare>& getSharedWatchers() {
|
|
19
|
+
static std::unordered_set<WatcherRef , WatcherHash, WatcherCompare>* sharedWatchers =
|
|
20
|
+
new std::unordered_set<WatcherRef , WatcherHash, WatcherCompare>();
|
|
21
|
+
return *sharedWatchers;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
WatcherRef Watcher::getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs) {
|
|
25
|
+
WatcherRef watcher = std::make_shared<Watcher>(dir, ignorePaths, ignoreGlobs);
|
|
26
|
+
auto found = getSharedWatchers().find(watcher);
|
|
27
|
+
if (found != getSharedWatchers().end()) {
|
|
28
|
+
return *found;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getSharedWatchers().insert(watcher);
|
|
32
|
+
return watcher;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
void removeShared(Watcher *watcher) {
|
|
36
|
+
for (auto it = getSharedWatchers().begin(); it != getSharedWatchers().end(); it++) {
|
|
37
|
+
if (it->get() == watcher) {
|
|
38
|
+
getSharedWatchers().erase(it);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Free up memory.
|
|
44
|
+
if (getSharedWatchers().size() == 0) {
|
|
45
|
+
getSharedWatchers().rehash(0);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
Watcher::Watcher(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs)
|
|
50
|
+
: mDir(dir),
|
|
51
|
+
mIgnorePaths(ignorePaths),
|
|
52
|
+
mIgnoreGlobs(ignoreGlobs) {
|
|
53
|
+
mDebounce = Debounce::getShared();
|
|
54
|
+
mDebounce->add(this, [this] () {
|
|
55
|
+
triggerCallbacks();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
Watcher::~Watcher() {
|
|
60
|
+
mDebounce->remove(this);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
void Watcher::wait() {
|
|
64
|
+
std::unique_lock<std::mutex> lk(mMutex);
|
|
65
|
+
mCond.wait(lk);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
void Watcher::notify() {
|
|
69
|
+
std::unique_lock<std::mutex> lk(mMutex);
|
|
70
|
+
mCond.notify_all();
|
|
71
|
+
|
|
72
|
+
if (mCallbacks.size() > 0 && mEvents.size() > 0) {
|
|
73
|
+
// We must release our lock before calling into the debouncer
|
|
74
|
+
// to avoid a deadlock: the debouncer thread itself will require
|
|
75
|
+
// our lock from its thread when calling into `triggerCallbacks`
|
|
76
|
+
// while holding its own debouncer lock.
|
|
77
|
+
lk.unlock();
|
|
78
|
+
mDebounce->trigger();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
struct CallbackData {
|
|
83
|
+
std::string error;
|
|
84
|
+
std::vector<Event> events;
|
|
85
|
+
CallbackData(std::string error, std::vector<Event> events) : error(error), events(events) {}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
Value callbackEventsToJS(const Env &env, std::vector<Event> &events) {
|
|
89
|
+
EscapableHandleScope scope(env);
|
|
90
|
+
Array arr = Array::New(env, events.size());
|
|
91
|
+
uint32_t currentEventIndex = 0;
|
|
92
|
+
for (auto eventIterator = events.begin(); eventIterator != events.end(); eventIterator++) {
|
|
93
|
+
arr.Set(currentEventIndex++, eventIterator->toJS(env));
|
|
94
|
+
}
|
|
95
|
+
return scope.Escape(arr);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
void callJSFunction(Napi::Env env, Function jsCallback, CallbackData *data) {
|
|
99
|
+
HandleScope scope(env);
|
|
100
|
+
auto err = data->error.size() > 0 ? Error::New(env, data->error).Value() : env.Null();
|
|
101
|
+
auto events = callbackEventsToJS(env, data->events);
|
|
102
|
+
jsCallback.Call({err, events});
|
|
103
|
+
delete data;
|
|
104
|
+
|
|
105
|
+
// Throw errors from the callback as fatal exceptions
|
|
106
|
+
// If we don't handle these node segfaults...
|
|
107
|
+
if (env.IsExceptionPending()) {
|
|
108
|
+
Napi::Error err = env.GetAndClearPendingException();
|
|
109
|
+
napi_fatal_exception(env, err.Value());
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void Watcher::notifyError(std::exception &err) {
|
|
114
|
+
std::unique_lock<std::mutex> lk(mMutex);
|
|
115
|
+
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
116
|
+
CallbackData *data = new CallbackData(err.what(), {});
|
|
117
|
+
it->tsfn.BlockingCall(data, callJSFunction);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
clearCallbacks();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// This function is called from the debounce thread.
|
|
124
|
+
void Watcher::triggerCallbacks() {
|
|
125
|
+
std::unique_lock<std::mutex> lk(mMutex);
|
|
126
|
+
if (mCallbacks.size() > 0 && (mEvents.size() > 0 || mEvents.hasError())) {
|
|
127
|
+
auto error = mEvents.getError();
|
|
128
|
+
auto events = mEvents.getEvents();
|
|
129
|
+
mEvents.clear();
|
|
130
|
+
|
|
131
|
+
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
132
|
+
it->tsfn.BlockingCall(new CallbackData(error, events), callJSFunction);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// This should be called from the JavaScript thread.
|
|
138
|
+
bool Watcher::watch(Function callback) {
|
|
139
|
+
std::unique_lock<std::mutex> lk(mMutex);
|
|
140
|
+
|
|
141
|
+
auto it = findCallback(callback);
|
|
142
|
+
if (it != mCallbacks.end()) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
auto tsfn = ThreadSafeFunction::New(
|
|
147
|
+
callback.Env(),
|
|
148
|
+
callback,
|
|
149
|
+
"Watcher callback",
|
|
150
|
+
0, // Unlimited queue
|
|
151
|
+
1 // Initial thread count
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
mCallbacks.push_back(Callback {
|
|
155
|
+
tsfn,
|
|
156
|
+
Napi::Persistent(callback),
|
|
157
|
+
std::this_thread::get_id()
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// This should be called from the JavaScript thread.
|
|
164
|
+
std::vector<Callback>::iterator Watcher::findCallback(Function callback) {
|
|
165
|
+
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
166
|
+
// Only consider callbacks created by the same thread, or V8 will panic.
|
|
167
|
+
if (it->threadId == std::this_thread::get_id() && it->ref.Value() == callback) {
|
|
168
|
+
return it;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return mCallbacks.end();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// This should be called from the JavaScript thread.
|
|
176
|
+
bool Watcher::unwatch(Function callback) {
|
|
177
|
+
std::unique_lock<std::mutex> lk(mMutex);
|
|
178
|
+
|
|
179
|
+
bool removed = false;
|
|
180
|
+
auto it = findCallback(callback);
|
|
181
|
+
if (it != mCallbacks.end()) {
|
|
182
|
+
it->tsfn.Release();
|
|
183
|
+
it->ref.Unref();
|
|
184
|
+
mCallbacks.erase(it);
|
|
185
|
+
removed = true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (removed && mCallbacks.size() == 0) {
|
|
189
|
+
unref();
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
void Watcher::unref() {
|
|
197
|
+
if (mCallbacks.size() == 0) {
|
|
198
|
+
removeShared(this);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void Watcher::destroy() {
|
|
203
|
+
std::unique_lock<std::mutex> lk(mMutex);
|
|
204
|
+
clearCallbacks();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Private because it doesn't lock.
|
|
208
|
+
void Watcher::clearCallbacks() {
|
|
209
|
+
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
210
|
+
it->tsfn.Release();
|
|
211
|
+
it->ref.Unref();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
mCallbacks.clear();
|
|
215
|
+
unref();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
bool Watcher::isIgnored(std::string path) {
|
|
219
|
+
for (auto it = mIgnorePaths.begin(); it != mIgnorePaths.end(); it++) {
|
|
220
|
+
auto dir = *it + DIR_SEP;
|
|
221
|
+
if (*it == path || path.compare(0, dir.size(), dir) == 0) {
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
auto basePath = mDir + DIR_SEP;
|
|
227
|
+
|
|
228
|
+
if (path.rfind(basePath, 0) != 0) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
auto relativePath = path.substr(basePath.size());
|
|
233
|
+
|
|
234
|
+
for (auto it = mIgnoreGlobs.begin(); it != mIgnoreGlobs.end(); it++) {
|
|
235
|
+
if (it->isIgnored(relativePath)) {
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return false;
|
|
241
|
+
}
|
package/src/Watcher.hh
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#ifndef WATCHER_H
|
|
2
|
+
#define WATCHER_H
|
|
3
|
+
|
|
4
|
+
#include <condition_variable>
|
|
5
|
+
#include <unordered_set>
|
|
6
|
+
#include <set>
|
|
7
|
+
#include <node_api.h>
|
|
8
|
+
#include "Glob.hh"
|
|
9
|
+
#include "Event.hh"
|
|
10
|
+
#include "Debounce.hh"
|
|
11
|
+
#include "DirTree.hh"
|
|
12
|
+
#include "Signal.hh"
|
|
13
|
+
|
|
14
|
+
using namespace Napi;
|
|
15
|
+
|
|
16
|
+
struct Watcher;
|
|
17
|
+
using WatcherRef = std::shared_ptr<Watcher>;
|
|
18
|
+
|
|
19
|
+
struct Callback {
|
|
20
|
+
Napi::ThreadSafeFunction tsfn;
|
|
21
|
+
Napi::FunctionReference ref;
|
|
22
|
+
std::thread::id threadId;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
class WatcherState {
|
|
26
|
+
public:
|
|
27
|
+
virtual ~WatcherState() = default;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
struct Watcher {
|
|
31
|
+
std::string mDir;
|
|
32
|
+
std::unordered_set<std::string> mIgnorePaths;
|
|
33
|
+
std::unordered_set<Glob> mIgnoreGlobs;
|
|
34
|
+
EventList mEvents;
|
|
35
|
+
std::shared_ptr<WatcherState> state;
|
|
36
|
+
|
|
37
|
+
Watcher(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);
|
|
38
|
+
~Watcher();
|
|
39
|
+
|
|
40
|
+
bool operator==(const Watcher &other) const {
|
|
41
|
+
return mDir == other.mDir && mIgnorePaths == other.mIgnorePaths && mIgnoreGlobs == other.mIgnoreGlobs;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
void wait();
|
|
45
|
+
void notify();
|
|
46
|
+
void notifyError(std::exception &err);
|
|
47
|
+
bool watch(Function callback);
|
|
48
|
+
bool unwatch(Function callback);
|
|
49
|
+
void unref();
|
|
50
|
+
bool isIgnored(std::string path);
|
|
51
|
+
void destroy();
|
|
52
|
+
|
|
53
|
+
static WatcherRef getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);
|
|
54
|
+
|
|
55
|
+
private:
|
|
56
|
+
std::mutex mMutex;
|
|
57
|
+
std::condition_variable mCond;
|
|
58
|
+
std::vector<Callback> mCallbacks;
|
|
59
|
+
std::shared_ptr<Debounce> mDebounce;
|
|
60
|
+
|
|
61
|
+
std::vector<Callback>::iterator findCallback(Function callback);
|
|
62
|
+
void clearCallbacks();
|
|
63
|
+
void triggerCallbacks();
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
class WatcherError : public std::runtime_error {
|
|
67
|
+
public:
|
|
68
|
+
WatcherRef mWatcher;
|
|
69
|
+
WatcherError(std::string msg, WatcherRef watcher) : std::runtime_error(msg), mWatcher(watcher) {}
|
|
70
|
+
WatcherError(const char *msg, WatcherRef watcher) : std::runtime_error(msg), mWatcher(watcher) {}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
#endif
|