Skip to content

Switch to Napi from NAN #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 4, 2020
Merged
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
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"parserOptions": {
"ecmaVersion": 2018
},
"env": {
"es6": true,
"jasmine": true,
"mocha": true,
"node": true
},
"extends": "eslint:recommended",
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ on:
push:
branches:
- master
pull_request:
pull_request:

name: Run Tests

Expand All @@ -12,7 +12,7 @@ jobs:
name: Tests
strategy:
matrix:
node: [8, 10, 12]
node: [10, 12]
os: [windows-2016, ubuntu-16.04, ubuntu-18.04, macOS-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
36 changes: 19 additions & 17 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@
"sources": [
"src/NSFW.cpp",
"src/Queue.cpp",
"src/NativeInterface.cpp",
"includes/NSFW.h",
"includes/Queue.h",
"includes/NativeInterface.h"
"src/NativeInterface.cpp"
],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"includes"
"includes",
"<!@(node -p \"require('node-addon-api').include\")"
],
"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",
"includes/win32/Controller.h",
"includes/win32/Watcher.h"
"src/win32/Watcher.cpp"
],
"msvs_settings": {
"VCCLCompilerTool": {
Expand All @@ -32,14 +37,14 @@
}
}],
["OS=='mac'", {
"cflags+": ["-fvisibility-hidden"],
"sources": [
"src/osx/RunLoop.cpp",
"src/osx/FSEventsService.cpp",
"includes/osx/RunLoop.h",
"includes/osx/FSEventsService.h"
"src/osx/FSEventsService.cpp"
],
"xcode_settings": {
'MACOSX_DEPLOYMENT_TARGET': '10.7',
"GCC_SYMBOLS_PRIVATE_EXTERN": "YES", # -fvisibility=hidden
"MACOSX_DEPLOYMENT_TARGET": "10.7",
"OTHER_CFLAGS": [
"-std=c++11",
"-stdlib=libc++"
Expand All @@ -60,10 +65,7 @@
"sources": [
"src/linux/InotifyEventLoop.cpp",
"src/linux/InotifyTree.cpp",
"src/linux/InotifyService.cpp",
"includes/linux/InotifyEventLoop.h",
"includes/linux/InotifyTree.h",
"includes/linux/InotifyService.h"
"src/linux/InotifyService.cpp"
],
"cflags": [
"-Wno-unknown-pragmas",
Expand Down
103 changes: 52 additions & 51 deletions includes/NSFW.h
Original file line number Diff line number Diff line change
@@ -1,69 +1,70 @@
#ifndef NSFW_H
#define NSFW_H

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

using namespace Nan;
#include "./Queue.h"
#include "./NativeInterface.h"

class NSFW : public ObjectWrap {
public:
static NAN_MODULE_INIT(Init);
class NSFW : public Napi::ObjectWrap<NSFW> {
private:
static Napi::FunctionReference constructor;
static std::size_t instanceCount;
static bool gcEnabled;

static void fireErrorCallback(uv_async_t *handle);
static void fireEventCallback(uv_async_t *handle);
static void pollForEvents(void *arg);
std::unique_ptr<NativeInterface> mInterface;
Napi::ThreadSafeFunction mEventCallback;
Napi::ThreadSafeFunction mErrorCallback;
std::mutex mInterfaceLock;
std::string mPath;
std::shared_ptr<EventQueue> mQueue;
std::thread mPollThread;
std::atomic<bool> mRunning;
uint32_t mDebounceMS;

Persistent<v8::Object> mPersistentHandle;
private:
NSFW(uint32_t debounceMS, std::string path, Callback *eventCallback, Callback *errorCallback);
~NSFW();
class StartWorker: public Napi::AsyncWorker {
public:
StartWorker(Napi::Env env, NSFW *nsfw);
void Execute();
void OnOK();
Napi::Promise RunJob();

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;
private:
Napi::Promise::Deferred mDeferred;
bool mDidStartWatching;
NSFW *mNSFW;
bool mShouldUnref;
};

struct ErrorBaton {
NSFW *nsfw;
std::string error;
};
Napi::Value Start(const Napi::CallbackInfo &info);

static NAN_METHOD(JSNew);
class StopWorker: public Napi::AsyncWorker {
public:
StopWorker(Napi::Env env, NSFW *nsfw);
void Execute();
void OnOK();
Napi::Promise RunJob();

static NAN_METHOD(Start);
class StartWorker : public AsyncWorker {
public:
StartWorker(NSFW *nsfw, Callback *callback);
void Execute();
void HandleOKCallback();
private:
NSFW *mNSFW;
};
private:
Napi::Promise::Deferred mDeferred;
bool mDidStopWatching;
NSFW *mNSFW;
};

Napi::Value Stop(const Napi::CallbackInfo &info);

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

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

#endif
Loading