| 1 |
#ifndef EVENT_H |
| 2 |
#define EVENT_H |
| 3 |
|
| 4 |
#include <string> |
| 5 |
#include <node_api.h> |
| 6 |
#include "wasm/include.h" |
| 7 |
#include <napi.h> |
| 8 |
#include <mutex> |
| 9 |
#include <map> |
| 10 |
#include <optional> |
| 11 |
|
| 12 |
using namespace Napi; |
| 13 |
|
| 14 |
struct Event { |
| 15 |
std::string path; |
| 16 |
bool isCreated; |
| 17 |
bool isDeleted; |
| 18 |
Event(std::string path) : path(path), isCreated(false), isDeleted(false) {} |
| 19 |
|
| 20 |
Value toJS(const Env& env) { |
| 21 |
EscapableHandleScope scope(env); |
| 22 |
Object res = Object::New(env); |
| 23 |
std::string type = isCreated ? "create" : isDeleted ? "delete" : "update"; |
| 24 |
res.Set(String::New(env, "path"), String::New(env, path.c_str())); |
| 25 |
res.Set(String::New(env, "type"), String::New(env, type.c_str())); |
| 26 |
return scope.Escape(res); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
class EventList { |
| 31 |
public: |
| 32 |
void create(std::string path) { |
| 33 |
std::lock_guard<std::mutex> l(mMutex); |
| 34 |
Event *event = internalUpdate(path); |
| 35 |
if (event->isDeleted) { |
| 36 |
// Assume update event when rapidly removed and created |
| 37 |
// https://github.com/parcel-bundler/watcher/issues/72 |
| 38 |
event->isDeleted = false; |
| 39 |
} else { |
| 40 |
event->isCreated = true; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
Event *update(std::string path) { |
| 45 |
std::lock_guard<std::mutex> l(mMutex); |
| 46 |
return internalUpdate(path); |
| 47 |
} |
| 48 |
|
| 49 |
void remove(std::string path) { |
| 50 |
std::lock_guard<std::mutex> l(mMutex); |
| 51 |
Event *event = internalUpdate(path); |
| 52 |
event->isDeleted = true; |
| 53 |
} |
| 54 |
|
| 55 |
size_t size() { |
| 56 |
std::lock_guard<std::mutex> l(mMutex); |
| 57 |
return mEvents.size(); |
| 58 |
} |
| 59 |
|
| 60 |
std::vector<Event> getEvents() { |
| 61 |
std::lock_guard<std::mutex> l(mMutex); |
| 62 |
std::vector<Event> eventsCloneVector; |
| 63 |
for(auto it = mEvents.begin(); it != mEvents.end(); ++it) { |
| 64 |
if (!(it->second.isCreated && it->second.isDeleted)) { |
| 65 |
eventsCloneVector.push_back(it->second); |
| 66 |
} |
| 67 |
} |
| 68 |
return eventsCloneVector; |
| 69 |
} |
| 70 |
|
| 71 |
void clear() { |
| 72 |
std::lock_guard<std::mutex> l(mMutex); |
| 73 |
mEvents.clear(); |
| 74 |
mError.reset(); |
| 75 |
} |
| 76 |
|
| 77 |
void error(std::string err) { |
| 78 |
std::lock_guard<std::mutex> l(mMutex); |
| 79 |
if (!mError.has_value()) { |
| 80 |
mError.emplace(err); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
bool hasError() { |
| 85 |
std::lock_guard<std::mutex> l(mMutex); |
| 86 |
return mError.has_value(); |
| 87 |
} |
| 88 |
|
| 89 |
std::string getError() { |
| 90 |
std::lock_guard<std::mutex> l(mMutex); |
| 91 |
return mError.value_or(""); |
| 92 |
} |
| 93 |
|
| 94 |
private: |
| 95 |
mutable std::mutex mMutex; |
| 96 |
std::map<std::string, Event> mEvents; |
| 97 |
std::optional<std::string> mError; |
| 98 |
Event *internalUpdate(std::string path) { |
| 99 |
auto found = mEvents.find(path); |
| 100 |
if (found == mEvents.end()) { |
| 101 |
auto it = mEvents.emplace(path, Event(path)); |
| 102 |
return &it.first->second; |
| 103 |
} |
| 104 |
|
| 105 |
return &found->second; |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
#endif |