Skip to content

Commit a76415a

Browse files
authored
Merge pull request #129 from theia-ide/mp/options-typings
fix gc issue and update typings
2 parents 94e83f0 + 9fb2eec commit a76415a

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ declare module 'nsfw' {
6060
interface Options {
6161
/** time in milliseconds to debounce the event callback */
6262
debounceMS?: number;
63-
/** callback to fire in the case of errors */
64-
errorCallback: (err: any) => void
63+
/** callback to fire in the case of errors */
64+
errorCallback?: (err: any) => void
6565
}
6666
}
6767

js/spec/index-spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,31 @@ describe('Node Sentinel File Watcher', function() {
562562
watch = null;
563563
}
564564
});
565+
566+
it('Can pass falsy values to errorCallback', async function() {
567+
const ok = [undefined, null, 0, '', false];
568+
const notOk = [1, true, 'a', {}, []];
569+
await Promise.all([
570+
...ok.map(async errorCallback => nsfw(
571+
workDir,
572+
() => {},
573+
{ errorCallback }
574+
)),
575+
...notOk.map(async errorCallback => {
576+
try {
577+
await nsfw(
578+
workDir,
579+
() => {},
580+
{ errorCallback },
581+
);
582+
} catch (error) {
583+
assert.ok(error.message === 'options.errorCallback must be a function.');
584+
return;
585+
}
586+
assert.fail('nsfw must error if errorCallback is not a function nor a falsy value');
587+
})
588+
]);
589+
});
565590
});
566591

567592
describe('Stress', function() {
@@ -684,6 +709,15 @@ describe('Node Sentinel File Watcher', function() {
684709
describe('Garbage collection', function() {
685710
it('can garbage collect all instances', async function () {
686711
this.timeout(60000);
712+
let threw = false;
713+
try {
714+
// Try to get an error coming from the C++ constructor by passing a bad callback
715+
await nsfw(workDir, () => {}, { errorCallback: 'not a callback' });
716+
} catch (e) {
717+
threw = true;
718+
} if (!threw) {
719+
assert.fail('nsfw must throw when provided a bad callback');
720+
}
687721
while (nsfw.getAllocatedInstanceCount() > 0) {
688722
global.gc();
689723
await sleep(0);

src/NSFW.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ NSFW::NSFW(const Napi::CallbackInfo &info):
1212
mPath(""),
1313
mRunning(false)
1414
{
15-
if (gcEnabled) {
16-
instanceCount++;
17-
}
18-
1915
auto env = info.Env();
2016
if (info.Length() < 1 || !info[0].IsString()) {
2117
throw Napi::TypeError::New(env, "Must pass a string path as the first argument to NSFW.");
@@ -71,6 +67,10 @@ NSFW::NSFW(const Napi::CallbackInfo &info):
7167
1
7268
);
7369
}
70+
71+
if (gcEnabled) {
72+
instanceCount++;
73+
}
7474
}
7575

7676
NSFW::~NSFW() {

0 commit comments

Comments
 (0)