source: imaps-frontend/node_modules/@parcel/watcher/src/Watcher.hh@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 1.8 KB
Line 
1#ifndef WATCHER_H
2#define WATCHER_H
3
4#include <condition_variable>
5#include <unordered_set>
6#include <set>
7#include <node_api.h>
8#include "Glob.hh"
9#include "Event.hh"
10#include "Debounce.hh"
11#include "DirTree.hh"
12#include "Signal.hh"
13
14using namespace Napi;
15
16struct Watcher;
17using WatcherRef = std::shared_ptr<Watcher>;
18
19struct Callback {
20 Napi::ThreadSafeFunction tsfn;
21 Napi::FunctionReference ref;
22 std::thread::id threadId;
23};
24
25class WatcherState {
26public:
27 virtual ~WatcherState() = default;
28};
29
30struct Watcher {
31 std::string mDir;
32 std::unordered_set<std::string> mIgnorePaths;
33 std::unordered_set<Glob> mIgnoreGlobs;
34 EventList mEvents;
35 std::shared_ptr<WatcherState> state;
36
37 Watcher(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);
38 ~Watcher();
39
40 bool operator==(const Watcher &other) const {
41 return mDir == other.mDir && mIgnorePaths == other.mIgnorePaths && mIgnoreGlobs == other.mIgnoreGlobs;
42 }
43
44 void wait();
45 void notify();
46 void notifyError(std::exception &err);
47 bool watch(Function callback);
48 bool unwatch(Function callback);
49 void unref();
50 bool isIgnored(std::string path);
51 void destroy();
52
53 static WatcherRef getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);
54
55private:
56 std::mutex mMutex;
57 std::condition_variable mCond;
58 std::vector<Callback> mCallbacks;
59 std::shared_ptr<Debounce> mDebounce;
60
61 std::vector<Callback>::iterator findCallback(Function callback);
62 void clearCallbacks();
63 void triggerCallbacks();
64};
65
66class WatcherError : public std::runtime_error {
67public:
68 WatcherRef mWatcher;
69 WatcherError(std::string msg, WatcherRef watcher) : std::runtime_error(msg), mWatcher(watcher) {}
70 WatcherError(const char *msg, WatcherRef watcher) : std::runtime_error(msg), mWatcher(watcher) {}
71};
72
73#endif
Note: See TracBrowser for help on using the repository browser.