Skip to content

Commit

Permalink
Merge pull request #110 from Axosoft/revert/napi-changes
Browse files Browse the repository at this point in the history
Revert Napi changes
  • Loading branch information
implausible authored Apr 8, 2020
2 parents 8419cb0 + 05d1863 commit ca1fc0f
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 312 deletions.
38 changes: 18 additions & 20 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,22 @@
"sources": [
"src/NSFW.cpp",
"src/Queue.cpp",
"src/NativeInterface.cpp"
"src/NativeInterface.cpp",
"includes/NSFW.h",
"includes/Queue.h",
"includes/NativeInterface.h"
],
"include_dirs": [
"includes",
"<!@(node -p \"require('node-addon-api').include\")"
"<!(node -e \"require('nan')\")",
"includes"
],
"cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-fno-exceptions"],
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"CLANG_CXX_LIBRARY": "libc++",
"MACOSX_DEPLOYMENT_TARGET": "10.7",
},
"msvs_settings": {
"VCCLCompilerTool": { "ExceptionHandling": 1 },
},
"conditions": [
["OS=='win'", {
"sources": [
"src/win32/Controller.cpp",
"src/win32/Watcher.cpp"
"src/win32/Watcher.cpp",
"includes/win32/Controller.h",
"includes/win32/Watcher.h"
],
"msvs_settings": {
"VCCLCompilerTool": {
Expand All @@ -37,14 +32,14 @@
}
}],
["OS=='mac'", {
"cflags+": ["-fvisibility-hidden"],
"sources": [
"src/osx/RunLoop.cpp",
"src/osx/FSEventsService.cpp"
"src/osx/FSEventsService.cpp",
"includes/osx/RunLoop.h",
"includes/osx/FSEventsService.h"
],
"xcode_settings": {
"GCC_SYMBOLS_PRIVATE_EXTERN": "YES", # -fvisibility=hidden
"MACOSX_DEPLOYMENT_TARGET": "10.7",
'MACOSX_DEPLOYMENT_TARGET': '10.7',
"OTHER_CFLAGS": [
"-std=c++11",
"-stdlib=libc++"
Expand All @@ -65,7 +60,10 @@
"sources": [
"src/linux/InotifyEventLoop.cpp",
"src/linux/InotifyTree.cpp",
"src/linux/InotifyService.cpp"
"src/linux/InotifyService.cpp",
"includes/linux/InotifyEventLoop.h",
"includes/linux/InotifyTree.h",
"includes/linux/InotifyService.h"
],
"cflags": [
"-Wno-unknown-pragmas",
Expand Down Expand Up @@ -102,7 +100,7 @@
],
"libraries": [
"-L/usr/local/lib",
"-linotify"
"-linotify"
]
}],
]
Expand Down
103 changes: 51 additions & 52 deletions includes/NSFW.h
Original file line number Diff line number Diff line change
@@ -1,70 +1,69 @@
#ifndef NSFW_H
#define NSFW_H

#include <atomic>
#include <chrono>
#include <memory>
#include <napi.h>
#include <thread>
#include "Queue.h"
#include "NativeInterface.h"
#include <nan.h>
#include <uv.h>
#include <vector>
#include <atomic>

#include "./Queue.h"
#include "./NativeInterface.h"

class NSFW : public Napi::ObjectWrap<NSFW> {
private:
static Napi::FunctionReference constructor;
static std::size_t instanceCount;
static bool gcEnabled;
using namespace Nan;

uint32_t mDebounceMS;
Napi::ThreadSafeFunction mErrorCallback;
Napi::ThreadSafeFunction mEventCallback;
std::unique_ptr<NativeInterface> mInterface;
std::mutex mInterfaceLock;
std::shared_ptr<EventQueue> mQueue;
std::string mPath;
std::thread mPollThread;
std::atomic<bool> mRunning;
class NSFW : public ObjectWrap {
public:
static NAN_MODULE_INIT(Init);

class StartWorker: public Napi::AsyncWorker {
public:
StartWorker(Napi::Env env, NSFW *nsfw);
void Execute();
void OnOK();
Napi::Promise RunJob();
static void fireErrorCallback(uv_async_t *handle);
static void fireEventCallback(uv_async_t *handle);
static void pollForEvents(void *arg);

private:
enum JobStatus { STARTED, ALREADY_RUNNING, COULD_NOT_START, JOB_NOT_EXECUTED_YET };
Napi::Promise::Deferred mDeferred;
NSFW *mNSFW;
JobStatus mStatus;
};
Persistent<v8::Object> mPersistentHandle;
private:
NSFW(uint32_t debounceMS, std::string path, Callback *eventCallback, Callback *errorCallback);
~NSFW();

Napi::Value Start(const Napi::CallbackInfo &info);
uint32_t mDebounceMS;
uv_async_t mErrorCallbackAsync;
uv_async_t mEventCallbackAsync;
Callback *mErrorCallback;
Callback *mEventCallback;
NativeInterface *mInterface;
uv_mutex_t mInterfaceLock;
bool mInterfaceLockValid;
std::string mPath;
uv_thread_t mPollThread;
std::atomic<bool> mRunning;
std::shared_ptr<EventQueue> mQueue;

class StopWorker: public Napi::AsyncWorker {
public:
StopWorker(Napi::Env env, NSFW *nsfw);
void Execute();
void OnOK();
Napi::Promise RunJob();
struct ErrorBaton {
NSFW *nsfw;
std::string error;
};

private:
Napi::Promise::Deferred mDeferred;
bool mDidStopWatching;
NSFW *mNSFW;
};
static NAN_METHOD(JSNew);

Napi::Value Stop(const Napi::CallbackInfo &info);
static NAN_METHOD(Start);
class StartWorker : public AsyncWorker {
public:
StartWorker(NSFW *nsfw, Callback *callback);
void Execute();
void HandleOKCallback();
private:
NSFW *mNSFW;
};

static NAN_METHOD(Stop);
class StopWorker : public AsyncWorker {
public:
static Napi::Object Init(Napi::Env, Napi::Object exports);
static Napi::Value InstanceCount(const Napi::CallbackInfo &info);
void pollForEvents();
StopWorker(NSFW *nsfw, Callback *callback);
void Execute();
void HandleOKCallback();
private:
NSFW *mNSFW;
};

NSFW(const Napi::CallbackInfo &info);
~NSFW();
static Persistent<v8::Function> constructor;
};

#endif
10 changes: 0 additions & 10 deletions js/spec/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,14 +673,4 @@ describe('Node Sentinel File Watcher', function() {
}
});
});

describe('Garbage collection', function() {
it('can garbage collect all instances', async function () {
this.timeout(60000);
while (nsfw.getAllocatedInstanceCount() > 0) {
global.gc();
await sleep(0);
}
});
});
});
15 changes: 10 additions & 5 deletions js/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { promises: fs } = require('fs');
const path = require('path');
const { promisify } = require('util');

const NSFW = require('../../build/Release/nsfw.node');
const { NSFW } = require('../../build/Release/nsfw.node');

function NSFWFilePoller(watchPath, eventCallback, debounceMS) {
const { CREATED, DELETED, MODIFIED } = nsfw.actions;
Expand Down Expand Up @@ -71,7 +72,7 @@ const buildNSFW = async (watchPath, eventCallback, { debounceMS = 500, errorCall
}

if (stats.isDirectory()) {
return new NSFW(watchPath, eventCallback, { debounceMS, errorCallback });
return new NSFW(debounceMS, watchPath, eventCallback, errorCallback);
} else if (stats.isFile()) {
return new NSFWFilePoller(watchPath, eventCallback, debounceMS);
} else {
Expand All @@ -85,9 +86,13 @@ function nsfw(watchPath, eventCallback, options) {
}

const implementation = watchPath;

this.start = () => implementation.start();
this.stop = () => implementation.stop();
if (implementation instanceof NSFW) {
this.start = promisify((callback) => implementation.start(callback));
this.stop = promisify((callback) => implementation.stop(callback));
} else {
this.start = () => implementation.start();
this.stop = () => implementation.stop();
}
}

nsfw.actions = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"homepage": "https://github.com/axosoft/node-simple-file-watcher",
"dependencies": {
"node-addon-api": "*"
"nan": "^2.0.0"
},
"devDependencies": {
"eslint": "^6.8.0",
Expand Down
Loading

0 comments on commit ca1fc0f

Please sign in to comment.