HEX
Server: nginx/1.24.0
System: Linux nowruzgan 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 x86_64
User: babak (1000)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/dev/math/ui/node_modules/@parcel/watcher/src/Debounce.hh
#ifndef DEBOUNCE_H
#define DEBOUNCE_H

#include <thread>
#include <unordered_map>
#include <functional>
#include "Signal.hh"

#define MIN_WAIT_TIME 50
#define MAX_WAIT_TIME 500

#ifdef __wasm32__
extern "C" {
  int set_timeout(int ms, void *ctx);
  void clear_timeout(int timeout);
  void on_timeout(void *ctx);
};
#endif

class Debounce {
public:
  static std::shared_ptr<Debounce> getShared();

  Debounce();
  ~Debounce();

  void add(void *key, std::function<void()> cb);
  void remove(void *key);
  void trigger();
  void notify();

private:
  bool mRunning;
  std::mutex mMutex;
  #ifdef __wasm32__
    int mTimeout;
  #else
    Signal mWaitSignal;
    std::thread mThread;
  #endif
  std::unordered_map<void *, std::function<void()>> mCallbacks;
  std::chrono::time_point<std::chrono::steady_clock> mLastTime;

  void loop();
  void notifyIfReady();
  void wait();
};

#endif