fsevents.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. ** © 2014 by Philipp Dunkel <pip@pipobscure.com>
  3. ** Licensed under MIT License.
  4. */
  5. #include "nan.h"
  6. #include "uv.h"
  7. #include "v8.h"
  8. #include "CoreFoundation/CoreFoundation.h"
  9. #include "CoreServices/CoreServices.h"
  10. #include <iostream>
  11. #include <vector>
  12. #include "src/storage.cc"
  13. namespace fse {
  14. class FSEvents : public Nan::ObjectWrap {
  15. public:
  16. explicit FSEvents(const char *path);
  17. ~FSEvents();
  18. uv_mutex_t mutex;
  19. // async.cc
  20. uv_async_t async;
  21. void asyncStart();
  22. void asyncTrigger();
  23. void asyncStop();
  24. // thread.cc
  25. uv_thread_t thread;
  26. CFRunLoopRef threadloop;
  27. void threadStart();
  28. static void threadRun(void *ctx);
  29. void threadStop();
  30. // methods.cc - internal
  31. Nan::AsyncResource async_resource;
  32. void emitEvent(const char *path, UInt32 flags, UInt64 id);
  33. // Common
  34. CFArrayRef paths;
  35. std::vector<fse_event*> events;
  36. static void Initialize(v8::Local<v8::Object> exports);
  37. // methods.cc - exposed
  38. static NAN_METHOD(New);
  39. static NAN_METHOD(Stop);
  40. static NAN_METHOD(Start);
  41. };
  42. }
  43. using namespace fse;
  44. FSEvents::FSEvents(const char *path)
  45. : async_resource("fsevents:FSEvents") {
  46. CFStringRef dirs[] = { CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8) };
  47. paths = CFArrayCreate(NULL, (const void **)&dirs, 1, NULL);
  48. threadloop = NULL;
  49. if (uv_mutex_init(&mutex)) abort();
  50. }
  51. FSEvents::~FSEvents() {
  52. CFRelease(paths);
  53. uv_mutex_destroy(&mutex);
  54. }
  55. #ifndef kFSEventStreamEventFlagItemCreated
  56. #define kFSEventStreamEventFlagItemCreated 0x00000010
  57. #endif
  58. #include "src/async.cc"
  59. #include "src/thread.cc"
  60. #include "src/constants.cc"
  61. #include "src/methods.cc"
  62. void FSEvents::Initialize(v8::Local<v8::Object> exports) {
  63. v8::Isolate* isolate = exports->GetIsolate();
  64. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  65. v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(FSEvents::New);
  66. tpl->SetClassName(Nan::New<v8::String>("FSEvents").ToLocalChecked());
  67. tpl->InstanceTemplate()->SetInternalFieldCount(1);
  68. Nan::SetPrototypeTemplate(tpl, "start", Nan::New<v8::FunctionTemplate>(FSEvents::Start));
  69. Nan::SetPrototypeTemplate(tpl, "stop", Nan::New<v8::FunctionTemplate>(FSEvents::Stop));
  70. Nan::Set(exports, Nan::New<v8::String>("Constants").ToLocalChecked(), Constants());
  71. Nan::Set(exports, Nan::New<v8::String>("FSEvents").ToLocalChecked(), tpl->GetFunction(context).ToLocalChecked());
  72. }
  73. NODE_MODULE(fse, FSEvents::Initialize)