Projekt

Obecné

Profil

Stáhnout (2.44 KB) Statistiky
| Větev: | Revize:
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 "CoreFoundation/CoreFoundation.h"
10
#include "CoreServices/CoreServices.h"
11
#include <iostream>
12
#include <vector>
13

    
14
#include "src/storage.cc"
15
namespace fse {
16
  class FSEvents : public Nan::ObjectWrap {
17
  public:
18
    explicit FSEvents(const char *path);
19
    ~FSEvents();
20

    
21
    uv_mutex_t mutex;
22

    
23
    // async.cc
24
    uv_async_t async;
25
    void asyncStart();
26
    void asyncTrigger();
27
    void asyncStop();
28

    
29
    // thread.cc
30
    uv_thread_t thread;
31
    CFRunLoopRef threadloop;
32
    void threadStart();
33
    static void threadRun(void *ctx);
34
    void threadStop();
35

    
36
    // methods.cc - internal
37
    Nan::AsyncResource async_resource;
38
    void emitEvent(const char *path, UInt32 flags, UInt64 id);
39

    
40
    // Common
41
    CFArrayRef paths;
42
    std::vector<fse_event*> events;
43
    static void Initialize(v8::Local<v8::Object> exports);
44

    
45
    // methods.cc - exposed
46
    static NAN_METHOD(New);
47
    static NAN_METHOD(Stop);
48
    static NAN_METHOD(Start);
49

    
50
  };
51
}
52

    
53
using namespace fse;
54

    
55
FSEvents::FSEvents(const char *path)
56
   : async_resource("fsevents:FSEvents") {
57
  CFStringRef dirs[] = { CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8) };
58
  paths = CFArrayCreate(NULL, (const void **)&dirs, 1, NULL);
59
  threadloop = NULL;
60
  if (uv_mutex_init(&mutex)) abort();
61
}
62
FSEvents::~FSEvents() {
63
  CFRelease(paths);
64
  uv_mutex_destroy(&mutex);
65
}
66

    
67
#ifndef kFSEventStreamEventFlagItemCreated
68
#define kFSEventStreamEventFlagItemCreated 0x00000010
69
#endif
70

    
71
#include "src/async.cc"
72
#include "src/thread.cc"
73
#include "src/constants.cc"
74
#include "src/methods.cc"
75

    
76
void FSEvents::Initialize(v8::Local<v8::Object> exports) {
77
  v8::Isolate* isolate = exports->GetIsolate();
78
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
79
  v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(FSEvents::New);
80
  tpl->SetClassName(Nan::New<v8::String>("FSEvents").ToLocalChecked());
81
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
82
  Nan::SetPrototypeTemplate(tpl, "start", Nan::New<v8::FunctionTemplate>(FSEvents::Start));
83
  Nan::SetPrototypeTemplate(tpl, "stop", Nan::New<v8::FunctionTemplate>(FSEvents::Stop));
84
  Nan::Set(exports, Nan::New<v8::String>("Constants").ToLocalChecked(), Constants());
85
  Nan::Set(exports, Nan::New<v8::String>("FSEvents").ToLocalChecked(), tpl->GetFunction(context).ToLocalChecked());
86
}
87

    
88
NODE_MODULE(fse, FSEvents::Initialize)
(6-6/8)