Skip to content

Commit ca1fc0f

Browse files
authoredApr 8, 2020
Merge pull request #110 from Axosoft/revert/napi-changes
Revert Napi changes
2 parents 8419cb0 + 05d1863 commit ca1fc0f

File tree

7 files changed

+331
-312
lines changed

7 files changed

+331
-312
lines changed
 

‎binding.gyp

+18-20
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,22 @@
55
"sources": [
66
"src/NSFW.cpp",
77
"src/Queue.cpp",
8-
"src/NativeInterface.cpp"
8+
"src/NativeInterface.cpp",
9+
"includes/NSFW.h",
10+
"includes/Queue.h",
11+
"includes/NativeInterface.h"
912
],
1013
"include_dirs": [
11-
"includes",
12-
"<!@(node -p \"require('node-addon-api').include\")"
14+
"<!(node -e \"require('nan')\")",
15+
"includes"
1316
],
14-
"cflags!": ["-fno-exceptions"],
15-
"cflags_cc!": ["-fno-exceptions"],
16-
"xcode_settings": {
17-
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
18-
"CLANG_CXX_LIBRARY": "libc++",
19-
"MACOSX_DEPLOYMENT_TARGET": "10.7",
20-
},
21-
"msvs_settings": {
22-
"VCCLCompilerTool": { "ExceptionHandling": 1 },
23-
},
2417
"conditions": [
2518
["OS=='win'", {
2619
"sources": [
2720
"src/win32/Controller.cpp",
28-
"src/win32/Watcher.cpp"
21+
"src/win32/Watcher.cpp",
22+
"includes/win32/Controller.h",
23+
"includes/win32/Watcher.h"
2924
],
3025
"msvs_settings": {
3126
"VCCLCompilerTool": {
@@ -37,14 +32,14 @@
3732
}
3833
}],
3934
["OS=='mac'", {
40-
"cflags+": ["-fvisibility-hidden"],
4135
"sources": [
4236
"src/osx/RunLoop.cpp",
43-
"src/osx/FSEventsService.cpp"
37+
"src/osx/FSEventsService.cpp",
38+
"includes/osx/RunLoop.h",
39+
"includes/osx/FSEventsService.h"
4440
],
4541
"xcode_settings": {
46-
"GCC_SYMBOLS_PRIVATE_EXTERN": "YES", # -fvisibility=hidden
47-
"MACOSX_DEPLOYMENT_TARGET": "10.7",
42+
'MACOSX_DEPLOYMENT_TARGET': '10.7',
4843
"OTHER_CFLAGS": [
4944
"-std=c++11",
5045
"-stdlib=libc++"
@@ -65,7 +60,10 @@
6560
"sources": [
6661
"src/linux/InotifyEventLoop.cpp",
6762
"src/linux/InotifyTree.cpp",
68-
"src/linux/InotifyService.cpp"
63+
"src/linux/InotifyService.cpp",
64+
"includes/linux/InotifyEventLoop.h",
65+
"includes/linux/InotifyTree.h",
66+
"includes/linux/InotifyService.h"
6967
],
7068
"cflags": [
7169
"-Wno-unknown-pragmas",
@@ -102,7 +100,7 @@
102100
],
103101
"libraries": [
104102
"-L/usr/local/lib",
105-
"-linotify"
103+
"-linotify"
106104
]
107105
}],
108106
]

‎includes/NSFW.h

+51-52
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,69 @@
11
#ifndef NSFW_H
22
#define NSFW_H
33

4-
#include <atomic>
5-
#include <chrono>
6-
#include <memory>
7-
#include <napi.h>
8-
#include <thread>
4+
#include "Queue.h"
5+
#include "NativeInterface.h"
6+
#include <nan.h>
7+
#include <uv.h>
98
#include <vector>
9+
#include <atomic>
1010

11-
#include "./Queue.h"
12-
#include "./NativeInterface.h"
13-
14-
class NSFW : public Napi::ObjectWrap<NSFW> {
15-
private:
16-
static Napi::FunctionReference constructor;
17-
static std::size_t instanceCount;
18-
static bool gcEnabled;
11+
using namespace Nan;
1912

20-
uint32_t mDebounceMS;
21-
Napi::ThreadSafeFunction mErrorCallback;
22-
Napi::ThreadSafeFunction mEventCallback;
23-
std::unique_ptr<NativeInterface> mInterface;
24-
std::mutex mInterfaceLock;
25-
std::shared_ptr<EventQueue> mQueue;
26-
std::string mPath;
27-
std::thread mPollThread;
28-
std::atomic<bool> mRunning;
13+
class NSFW : public ObjectWrap {
14+
public:
15+
static NAN_MODULE_INIT(Init);
2916

30-
class StartWorker: public Napi::AsyncWorker {
31-
public:
32-
StartWorker(Napi::Env env, NSFW *nsfw);
33-
void Execute();
34-
void OnOK();
35-
Napi::Promise RunJob();
17+
static void fireErrorCallback(uv_async_t *handle);
18+
static void fireEventCallback(uv_async_t *handle);
19+
static void pollForEvents(void *arg);
3620

37-
private:
38-
enum JobStatus { STARTED, ALREADY_RUNNING, COULD_NOT_START, JOB_NOT_EXECUTED_YET };
39-
Napi::Promise::Deferred mDeferred;
40-
NSFW *mNSFW;
41-
JobStatus mStatus;
42-
};
21+
Persistent<v8::Object> mPersistentHandle;
22+
private:
23+
NSFW(uint32_t debounceMS, std::string path, Callback *eventCallback, Callback *errorCallback);
24+
~NSFW();
4325

44-
Napi::Value Start(const Napi::CallbackInfo &info);
26+
uint32_t mDebounceMS;
27+
uv_async_t mErrorCallbackAsync;
28+
uv_async_t mEventCallbackAsync;
29+
Callback *mErrorCallback;
30+
Callback *mEventCallback;
31+
NativeInterface *mInterface;
32+
uv_mutex_t mInterfaceLock;
33+
bool mInterfaceLockValid;
34+
std::string mPath;
35+
uv_thread_t mPollThread;
36+
std::atomic<bool> mRunning;
37+
std::shared_ptr<EventQueue> mQueue;
4538

46-
class StopWorker: public Napi::AsyncWorker {
47-
public:
48-
StopWorker(Napi::Env env, NSFW *nsfw);
49-
void Execute();
50-
void OnOK();
51-
Napi::Promise RunJob();
39+
struct ErrorBaton {
40+
NSFW *nsfw;
41+
std::string error;
42+
};
5243

53-
private:
54-
Napi::Promise::Deferred mDeferred;
55-
bool mDidStopWatching;
56-
NSFW *mNSFW;
57-
};
44+
static NAN_METHOD(JSNew);
5845

59-
Napi::Value Stop(const Napi::CallbackInfo &info);
46+
static NAN_METHOD(Start);
47+
class StartWorker : public AsyncWorker {
48+
public:
49+
StartWorker(NSFW *nsfw, Callback *callback);
50+
void Execute();
51+
void HandleOKCallback();
52+
private:
53+
NSFW *mNSFW;
54+
};
6055

56+
static NAN_METHOD(Stop);
57+
class StopWorker : public AsyncWorker {
6158
public:
62-
static Napi::Object Init(Napi::Env, Napi::Object exports);
63-
static Napi::Value InstanceCount(const Napi::CallbackInfo &info);
64-
void pollForEvents();
59+
StopWorker(NSFW *nsfw, Callback *callback);
60+
void Execute();
61+
void HandleOKCallback();
62+
private:
63+
NSFW *mNSFW;
64+
};
6565

66-
NSFW(const Napi::CallbackInfo &info);
67-
~NSFW();
66+
static Persistent<v8::Function> constructor;
6867
};
6968

7069
#endif

‎js/spec/index-spec.js

-10
Original file line numberDiff line numberDiff line change
@@ -673,14 +673,4 @@ describe('Node Sentinel File Watcher', function() {
673673
}
674674
});
675675
});
676-
677-
describe('Garbage collection', function() {
678-
it('can garbage collect all instances', async function () {
679-
this.timeout(60000);
680-
while (nsfw.getAllocatedInstanceCount() > 0) {
681-
global.gc();
682-
await sleep(0);
683-
}
684-
});
685-
});
686676
});

‎js/src/index.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { promises: fs } = require('fs');
22
const path = require('path');
3+
const { promisify } = require('util');
34

4-
const NSFW = require('../../build/Release/nsfw.node');
5+
const { NSFW } = require('../../build/Release/nsfw.node');
56

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

7374
if (stats.isDirectory()) {
74-
return new NSFW(watchPath, eventCallback, { debounceMS, errorCallback });
75+
return new NSFW(debounceMS, watchPath, eventCallback, errorCallback);
7576
} else if (stats.isFile()) {
7677
return new NSFWFilePoller(watchPath, eventCallback, debounceMS);
7778
} else {
@@ -85,9 +86,13 @@ function nsfw(watchPath, eventCallback, options) {
8586
}
8687

8788
const implementation = watchPath;
88-
89-
this.start = () => implementation.start();
90-
this.stop = () => implementation.stop();
89+
if (implementation instanceof NSFW) {
90+
this.start = promisify((callback) => implementation.start(callback));
91+
this.stop = promisify((callback) => implementation.stop(callback));
92+
} else {
93+
this.start = () => implementation.start();
94+
this.stop = () => implementation.stop();
95+
}
9196
}
9297

9398
nsfw.actions = {

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"homepage": "https://github.com/axosoft/node-simple-file-watcher",
2828
"dependencies": {
29-
"node-addon-api": "*"
29+
"nan": "^2.0.0"
3030
},
3131
"devDependencies": {
3232
"eslint": "^6.8.0",

‎src/NSFW.cpp

+246-219
Large diffs are not rendered by default.

‎yarn.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,11 @@ mute-stream@0.0.8:
894894
resolved "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
895895
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==
896896

897+
nan@^2.0.0:
898+
version "2.14.0"
899+
resolved "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
900+
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==
901+
897902
natural-compare@^1.4.0:
898903
version "1.4.0"
899904
resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
@@ -904,11 +909,6 @@ nice-try@^1.0.4:
904909
resolved "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
905910
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
906911

907-
node-addon-api@*:
908-
version "2.0.0"
909-
resolved "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.0.tgz#f9afb8d777a91525244b01775ea0ddbe1125483b"
910-
integrity sha512-ASCL5U13as7HhOExbT6OlWJJUV/lLzL2voOSP1UVehpRD8FbSrSDjfScK/KwAvVTI5AS6r4VwbOMlIqtvRidnA==
911-
912912
node-environment-flags@1.0.6:
913913
version "1.0.6"
914914
resolved "https://registry.npmjs.org/node-environment-flags/-/node-environment-flags-1.0.6.tgz#a30ac13621f6f7d674260a54dede048c3982c088"

0 commit comments

Comments
 (0)
Please sign in to comment.