Skip to content

Commit

Permalink
More requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pdunkel committed Nov 10, 2018
1 parent abf71f8 commit 101ebf6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
},
"homepage": "https://github.com/strongloop/fsevents",
"devDependencies": {
"node-gyp": "*",
"tap": "~12.0.1"
}
}
47 changes: 25 additions & 22 deletions src/fsevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
#include "runner.h"
#include <node_api.h>

#define CONST(name, const_val) CHECK(napi_create_int32(env, const_val, &value) == napi_ok); CHECK(napi_set_named_property(env, constants, name, value) == napi_ok);
#define CONSTANT(name) do {\
CHECK(napi_create_int32(env, name, &value) == napi_ok);\
CHECK(napi_set_named_property(env, constants, #name, value) == napi_ok);\
} while (0)

// constants from https://developer.apple.com/library/mac/documentation/Darwin/Reference/FSEvents_Ref/index.html#//apple_ref/doc/constant_group/FSEventStreamEventFlags
#ifndef kFSEventStreamEventFlagNone
Expand Down Expand Up @@ -121,7 +124,7 @@ static napi_value FSEStart(napi_env env, napi_callback_info info) {

CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(napi_get_value_string_utf8(env, argv[0], path, PATH_MAX, &argc) == 0);
return start(env, (const char (*)[PATH_MAX]) path, argv[1]);
return start(env, &path, argv[1]);
}
static napi_value FSEStop(napi_env env, napi_callback_info info) {
size_t argc = 1;
Expand All @@ -139,26 +142,26 @@ napi_value Init(napi_env env, napi_value exports) {
napi_value value;
CHECK(napi_create_object(env, &constants) == napi_ok);

CONST("kFSEventStreamEventFlagNone", kFSEventStreamEventFlagNone)
CONST("kFSEventStreamEventFlagMustScanSubDirs", kFSEventStreamEventFlagMustScanSubDirs)
CONST("kFSEventStreamEventFlagUserDropped", kFSEventStreamEventFlagMustScanSubDirs)
CONST("kFSEventStreamEventFlagKernelDropped", kFSEventStreamEventFlagKernelDropped)
CONST("kFSEventStreamEventFlagEventIdsWrapped", kFSEventStreamEventFlagEventIdsWrapped)
CONST("kFSEventStreamEventFlagHistoryDone", kFSEventStreamEventFlagHistoryDone)
CONST("kFSEventStreamEventFlagRootChanged", kFSEventStreamEventFlagRootChanged)
CONST("kFSEventStreamEventFlagMount", kFSEventStreamEventFlagMount)
CONST("kFSEventStreamEventFlagUnmount", kFSEventStreamEventFlagUnmount)
CONST("kFSEventStreamEventFlagItemCreated", kFSEventStreamEventFlagItemCreated)
CONST("kFSEventStreamEventFlagItemRemoved", kFSEventStreamEventFlagItemRemoved)
CONST("kFSEventStreamEventFlagItemInodeMetaMod", kFSEventStreamEventFlagItemInodeMetaMod)
CONST("kFSEventStreamEventFlagItemRenamed", kFSEventStreamEventFlagItemRenamed)
CONST("kFSEventStreamEventFlagItemModified", kFSEventStreamEventFlagItemModified)
CONST("kFSEventStreamEventFlagItemFinderInfoMod", kFSEventStreamEventFlagItemFinderInfoMod)
CONST("kFSEventStreamEventFlagItemChangeOwner", kFSEventStreamEventFlagItemChangeOwner)
CONST("kFSEventStreamEventFlagItemXattrMod", kFSEventStreamEventFlagItemXattrMod)
CONST("kFSEventStreamEventFlagItemIsFile", kFSEventStreamEventFlagItemIsFile)
CONST("kFSEventStreamEventFlagItemIsDir", kFSEventStreamEventFlagItemIsDir)
CONST("kFSEventStreamEventFlagItemIsSymlink", kFSEventStreamEventFlagItemIsSymlink)
CONSTANT(kFSEventStreamEventFlagNone);
CONSTANT(kFSEventStreamEventFlagMustScanSubDirs);
CONSTANT(kFSEventStreamEventFlagUserDropped);
CONSTANT(kFSEventStreamEventFlagKernelDropped);
CONSTANT(kFSEventStreamEventFlagEventIdsWrapped);
CONSTANT(kFSEventStreamEventFlagHistoryDone);
CONSTANT(kFSEventStreamEventFlagRootChanged);
CONSTANT(kFSEventStreamEventFlagMount);
CONSTANT(kFSEventStreamEventFlagUnmount);
CONSTANT(kFSEventStreamEventFlagItemCreated);
CONSTANT(kFSEventStreamEventFlagItemRemoved);
CONSTANT(kFSEventStreamEventFlagItemInodeMetaMod);
CONSTANT(kFSEventStreamEventFlagItemRenamed);
CONSTANT(kFSEventStreamEventFlagItemModified);
CONSTANT(kFSEventStreamEventFlagItemFinderInfoMod);
CONSTANT(kFSEventStreamEventFlagItemChangeOwner);
CONSTANT(kFSEventStreamEventFlagItemXattrMod);
CONSTANT(kFSEventStreamEventFlagItemIsFile);
CONSTANT(kFSEventStreamEventFlagItemIsDir);
CONSTANT(kFSEventStreamEventFlagItemIsSymlink);

napi_property_descriptor descriptors[] = {
{ "Constants", NULL, NULL, NULL, NULL, constants, napi_default, NULL },
Expand Down
20 changes: 13 additions & 7 deletions src/runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void handleEvents(
instance->tail = event;
}
}
uv_async_send(&instance->async);
uv_async_send(instance->async);
uv_mutex_unlock(&instance->mutex);
}

Expand Down Expand Up @@ -94,39 +94,45 @@ void execute(void *data) {
instance->loop = NULL;
}

void cleanup(napi_env env, void* data, void* ignored) {
void napi_cleanup(napi_env env, void* data, void* ignored) {
fse_t *instance = (fse_t*)data;
stop(instance);
CHECK(napi_delete_reference(instance->env, instance->callback) == napi_ok);
free(instance);
}
void async_cleanup(uv_async_t* handle) {
free(handle);
}

void stop(fse_t *instance) {
if (!instance->loop) return;
unsigned int count = 0;
CFRunLoopStop(instance->loop);
CHECK(!uv_thread_join(&instance->thread));
instance->async.data = NULL;
uv_close((uv_handle_t *) &instance->async, NULL);
instance->async->data = NULL;
uv_mutex_destroy(&instance->mutex);
uv_close((uv_handle_t *) instance->async, async_cleanup);
instance->async = NULL;
CHECK(napi_reference_unref(instance->env, instance->callback, &count) == napi_ok);
}

napi_value start(napi_env env, const char (*path)[PATH_MAX], napi_value callback) {
napi_value result;
fse_t *instance = malloc(sizeof(*instance));
instance->async = malloc(sizeof(*instance->async));

memcpy(instance->path, path, PATH_MAX);

instance->env = env;
instance->head = NULL;
instance->tail = NULL;

CHECK(napi_create_reference(env, callback, 1, &instance->callback) == napi_ok);
CHECK(!uv_async_init(uv_default_loop(), &instance->async, (uv_async_cb) eventCallback));
CHECK(!uv_async_init(uv_default_loop(), instance->async, (uv_async_cb) eventCallback));
CHECK(!uv_mutex_init(&instance->mutex));
instance->async.data = instance;
instance->async->data = instance;
CHECK(!uv_thread_create(&instance->thread, execute, (void*)instance));

CHECK(napi_create_external(env, instance, cleanup, NULL, &result) == napi_ok);
CHECK(napi_create_external(env, instance, napi_cleanup, NULL, &result) == napi_ok);
return result;
}
2 changes: 1 addition & 1 deletion src/runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef struct {

CFRunLoopRef loop;
uv_thread_t thread;
uv_async_t async;
uv_async_t *async;
uv_mutex_t mutex;

void *head;
Expand Down

0 comments on commit 101ebf6

Please sign in to comment.