Event.hh 2.5 KB

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