[0c6b92a] | 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 |
|
---|
| 11 | using namespace Napi;
|
---|
| 12 |
|
---|
| 13 | struct Event {
|
---|
| 14 | std::string path;
|
---|
| 15 | bool isCreated;
|
---|
| 16 | bool isDeleted;
|
---|
| 17 | Event(std::string path) : path(path), isCreated(false), isDeleted(false) {}
|
---|
| 18 |
|
---|
| 19 | Value toJS(const Env& env) {
|
---|
| 20 | EscapableHandleScope scope(env);
|
---|
| 21 | Object res = Object::New(env);
|
---|
| 22 | std::string type = isCreated ? "create" : isDeleted ? "delete" : "update";
|
---|
| 23 | res.Set(String::New(env, "path"), String::New(env, path.c_str()));
|
---|
| 24 | res.Set(String::New(env, "type"), String::New(env, type.c_str()));
|
---|
| 25 | return scope.Escape(res);
|
---|
| 26 | }
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | class EventList {
|
---|
| 30 | public:
|
---|
| 31 | void create(std::string path) {
|
---|
| 32 | std::lock_guard<std::mutex> l(mMutex);
|
---|
| 33 | Event *event = internalUpdate(path);
|
---|
| 34 | if (event->isDeleted) {
|
---|
| 35 | // Assume update event when rapidly removed and created
|
---|
| 36 | // https://github.com/parcel-bundler/watcher/issues/72
|
---|
| 37 | event->isDeleted = false;
|
---|
| 38 | } else {
|
---|
| 39 | event->isCreated = true;
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | Event *update(std::string path) {
|
---|
| 44 | std::lock_guard<std::mutex> l(mMutex);
|
---|
| 45 | return internalUpdate(path);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | void remove(std::string path) {
|
---|
| 49 | std::lock_guard<std::mutex> l(mMutex);
|
---|
| 50 | Event *event = internalUpdate(path);
|
---|
| 51 | event->isDeleted = true;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | size_t size() {
|
---|
| 55 | std::lock_guard<std::mutex> l(mMutex);
|
---|
| 56 | return mEvents.size();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | std::vector<Event> getEvents() {
|
---|
| 60 | std::lock_guard<std::mutex> l(mMutex);
|
---|
| 61 | std::vector<Event> eventsCloneVector;
|
---|
| 62 | for(auto it = mEvents.begin(); it != mEvents.end(); ++it) {
|
---|
| 63 | if (!(it->second.isCreated && it->second.isDeleted)) {
|
---|
| 64 | eventsCloneVector.push_back(it->second);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | return eventsCloneVector;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void clear() {
|
---|
| 71 | std::lock_guard<std::mutex> l(mMutex);
|
---|
| 72 | mEvents.clear();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | private:
|
---|
| 76 | mutable std::mutex mMutex;
|
---|
| 77 | std::map<std::string, Event> mEvents;
|
---|
| 78 | Event *internalUpdate(std::string path) {
|
---|
| 79 | auto found = mEvents.find(path);
|
---|
| 80 | if (found == mEvents.end()) {
|
---|
| 81 | auto it = mEvents.emplace(path, Event(path));
|
---|
| 82 | return &it.first->second;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return &found->second;
|
---|
| 86 | }
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | #endif
|
---|