| 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 |
} |