forked from fsevents/fsevents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsevents.cc
102 lines (83 loc) · 2.49 KB
/
fsevents.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
** © 2014 by Philipp Dunkel <[email protected]>
** Licensed under MIT License.
*/
#include "nan.h"
#include "uv.h"
#include "v8.h"
#include "pthread.h"
#include "CoreFoundation/CoreFoundation.h"
#include "CoreServices/CoreServices.h"
#include <iostream>
#include "src/storage.cc"
namespace fse {
class FSEvents : public node::ObjectWrap {
public:
FSEvents(const char *path, NanCallback *handler);
~FSEvents();
// locking.cc
bool lockStarted;
pthread_mutex_t lockmutex;
void lockingStart();
void lock();
void unlock();
void lockingStop();
// async.cc
uv_async_t async;
void asyncStart();
void asyncTrigger();
void asyncStop();
// thread.cc
bool threadStarted;
pthread_t thread;
CFRunLoopRef threadloop;
void threadStart();
static void *threadRun(void *ctx);
void threadStop();
// methods.cc - internal
NanCallback *handler;
void emitEvent(const char *path, UInt32 flags, UInt64 id);
// Common
CFArrayRef paths;
CFMutableArrayRef events;
static void Initialize(v8::Handle<v8::Object> exports);
// methods.cc - exposed
static NAN_METHOD(New);
static NAN_METHOD(Stop);
static NAN_METHOD(Start);
};
}
using namespace fse;
FSEvents::FSEvents(const char *path, NanCallback *handler): handler(handler) {
CFStringRef dirs[] = { CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8) };
paths = CFArrayCreate(NULL, (const void **)&dirs, 1, NULL);
events = CFArrayCreateMutable(NULL, 0, &FSEventArrayCallBacks);
threadloop = NULL;
lockingStart();
}
FSEvents::~FSEvents() {
std::cout << "YIKES" << std::endl;
lockingStop();
delete handler;
handler = NULL;
CFRelease(paths);
CFRelease(events);
}
#ifndef kFSEventStreamEventFlagItemCreated
#define kFSEventStreamEventFlagItemCreated 0x00000010
#endif
#include "src/locking.cc"
#include "src/async.cc"
#include "src/thread.cc"
#include "src/constants.cc"
#include "src/methods.cc"
void FSEvents::Initialize(v8::Handle<v8::Object> exports) {
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(FSEvents::New);
tpl->SetClassName(NanSymbol("FSEvents"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(tpl, "stop", FSEvents::Stop);
NODE_SET_PROTOTYPE_METHOD(tpl, "start", FSEvents::Start);
exports->Set(v8::String::NewSymbol("Constants"), Constants());
exports->Set(v8::String::NewSymbol("FSEvents"), tpl->GetFunction());
}
NODE_MODULE(fse, FSEvents::Initialize)