| 1 |
#include "Debounce.hh" |
| 2 |
|
| 3 |
#ifdef __wasm32__ |
| 4 |
extern "C" void on_timeout(void *ctx) { |
| 5 |
Debounce *debounce = (Debounce *)ctx; |
| 6 |
debounce->notify(); |
| 7 |
} |
| 8 |
#endif |
| 9 |
|
| 10 |
std::shared_ptr<Debounce> Debounce::getShared() { |
| 11 |
static std::weak_ptr<Debounce> sharedInstance; |
| 12 |
std::shared_ptr<Debounce> shared = sharedInstance.lock(); |
| 13 |
if (!shared) { |
| 14 |
shared = std::make_shared<Debounce>(); |
| 15 |
sharedInstance = shared; |
| 16 |
} |
| 17 |
|
| 18 |
return shared; |
| 19 |
} |
| 20 |
|
| 21 |
Debounce::Debounce() { |
| 22 |
mRunning = true; |
| 23 |
#ifndef __wasm32__ |
| 24 |
mThread = std::thread([this] () { |
| 25 |
loop(); |
| 26 |
}); |
| 27 |
#endif |
| 28 |
} |
| 29 |
|
| 30 |
Debounce::~Debounce() { |
| 31 |
mRunning = false; |
| 32 |
#ifndef __wasm32__ |
| 33 |
mWaitSignal.notify(); |
| 34 |
mThread.join(); |
| 35 |
#endif |
| 36 |
} |
| 37 |
|
| 38 |
void Debounce::add(void *key, std::function<void()> cb) { |
| 39 |
std::unique_lock<std::mutex> lock(mMutex); |
| 40 |
mCallbacks.emplace(key, cb); |
| 41 |
} |
| 42 |
|
| 43 |
void Debounce::remove(void *key) { |
| 44 |
std::unique_lock<std::mutex> lock(mMutex); |
| 45 |
mCallbacks.erase(key); |
| 46 |
} |
| 47 |
|
| 48 |
void Debounce::trigger() { |
| 49 |
std::unique_lock<std::mutex> lock(mMutex); |
| 50 |
#ifdef __wasm32__ |
| 51 |
notifyIfReady(); |
| 52 |
#else |
| 53 |
mWaitSignal.notify(); |
| 54 |
#endif |
| 55 |
} |
| 56 |
|
| 57 |
#ifndef __wasm32__ |
| 58 |
void Debounce::loop() { |
| 59 |
while (mRunning) { |
| 60 |
mWaitSignal.wait(); |
| 61 |
if (!mRunning) { |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
notifyIfReady(); |
| 66 |
} |
| 67 |
} |
| 68 |
#endif |
| 69 |
|
| 70 |
void Debounce::notifyIfReady() { |
| 71 |
if (!mRunning) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
// If we haven't seen an event in more than the maximum wait time, notify callbacks immediately |
| 76 |
// to ensure that we don't wait forever. Otherwise, wait for the minimum wait time and batch |
| 77 |
// subsequent fast changes. This also means the first file change in a batch is notified immediately, |
| 78 |
// separately from the rest of the batch. This seems like an acceptable tradeoff if the common case |
| 79 |
// is that only a single file was updated at a time. |
| 80 |
auto time = std::chrono::steady_clock::now(); |
| 81 |
if ((time - mLastTime) > std::chrono::milliseconds(MAX_WAIT_TIME)) { |
| 82 |
mLastTime = time; |
| 83 |
notify(); |
| 84 |
} else { |
| 85 |
wait(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
void Debounce::wait() { |
| 90 |
#ifdef __wasm32__ |
| 91 |
clear_timeout(mTimeout); |
| 92 |
mTimeout = set_timeout(MIN_WAIT_TIME, this); |
| 93 |
#else |
| 94 |
auto status = mWaitSignal.waitFor(std::chrono::milliseconds(MIN_WAIT_TIME)); |
| 95 |
if (mRunning && (status == std::cv_status::timeout)) { |
| 96 |
notify(); |
| 97 |
} |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
void Debounce::notify() { |
| 102 |
std::unique_lock<std::mutex> lock(mMutex); |
| 103 |
|
| 104 |
mLastTime = std::chrono::steady_clock::now(); |
| 105 |
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) { |
| 106 |
auto cb = it->second; |
| 107 |
cb(); |
| 108 |
} |
| 109 |
|
| 110 |
#ifndef __wasm32__ |
| 111 |
mWaitSignal.reset(); |
| 112 |
#endif |
| 113 |
} |