Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/NSFW.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class NSFW : public Napi::ObjectWrap<NSFW> {
};

Napi::Value UpdateExcludedPaths(const Napi::CallbackInfo &info);
Napi::Value Close(const Napi::CallbackInfo &info);

public:
static Napi::Object Init(Napi::Env, Napi::Object exports);
Expand Down
2 changes: 2 additions & 0 deletions js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function NSFWFilePoller(watchPath, eventCallback, debounceMS) {

this.pause = () => this.stop();
this.resume = () => this.start();
this.close = () => this.close();
}


Expand Down Expand Up @@ -112,6 +113,7 @@ function nsfw(watchPath, eventCallback, options) {
this.resume = () => implementation.resume();
this.getExcludedPaths = () => implementation.getExcludedPaths();
this.updateExcludedPaths = (paths) => implementation.updateExcludedPaths(paths);
this.close = () => implementation.close();
}


Expand Down
38 changes: 27 additions & 11 deletions src/NSFW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ void NSFW::StartWorker::Execute() {
std::lock_guard<std::mutex> lock(mNSFW->mRunningLock);
mNSFW->mRunning = true;
}
mNSFW->mErrorCallback.Acquire();
if (mNSFW->mErrorCallback) {
mNSFW->mErrorCallback.Acquire();
}
mNSFW->mEventCallback.Acquire();
mNSFW->mPollThread = std::thread([] (NSFW *nsfw) { nsfw->pollForEvents(); }, mNSFW);
} else {
Expand Down Expand Up @@ -388,14 +390,16 @@ void NSFW::pollForEvents() {
std::lock_guard<std::mutex> lock(mInterfaceLock);

if (mInterface->hasErrored()) {
const std::string &error = mInterface->getError();
mErrorCallback.NonBlockingCall([error](Napi::Env env, Napi::Function jsCallback) {
Napi::Value jsError = Napi::Error::New(env, error).Value();
jsCallback.Call({ jsError });
});
{
std::lock_guard<std::mutex> lock(mRunningLock);
mRunning = false;
if (mErrorCallback) {
const std::string &error = mInterface->getError();
mErrorCallback.NonBlockingCall([error](Napi::Env env, Napi::Function jsCallback) {
Napi::Value jsError = Napi::Error::New(env, error).Value();
jsCallback.Call({ jsError });
});
{
std::lock_guard<std::mutex> lock(mRunningLock);
mRunning = false;
}
}
break;
}
Expand Down Expand Up @@ -447,7 +451,9 @@ void NSFW::pollForEvents() {
// If we are destroying NFSW object (destructor) we cannot release the thread safe functions at this point
// or we get a segfault
if (!mFinalizing) {
mErrorCallback.Release();
if (mErrorCallback) {
mErrorCallback.Release();
}
mEventCallback.Release();
}
}
Expand All @@ -463,6 +469,14 @@ Napi::Value NSFW::ExcludedPaths() {
return path_array;
}

Napi::Value NSFW::Close(const Napi::CallbackInfo &info) {
mEventCallback.Release();
if (mErrorCallback) {
mErrorCallback.Release();
}
return Napi::Value();
}

Napi::Value NSFW::InstanceCount(const Napi::CallbackInfo &info) {
return Napi::Number::New(info.Env(), instanceCount);
}
Expand All @@ -476,9 +490,11 @@ Napi::Object NSFW::Init(Napi::Env env, Napi::Object exports) {
InstanceMethod("pause", &NSFW::Pause),
InstanceMethod("resume", &NSFW::Resume),
InstanceMethod("getExcludedPaths", &NSFW::GetExcludedPaths),
InstanceMethod("updateExcludedPaths", &NSFW::UpdateExcludedPaths)
InstanceMethod("updateExcludedPaths", &NSFW::UpdateExcludedPaths),
InstanceMethod("close", &NSFW::Close)
});


if (gcEnabled) {
nsfwConstructor.DefineProperty(Napi::PropertyDescriptor::Function(
"getAllocatedInstanceCount",
Expand Down