UNPKG

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