help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
ruby-calculator
/
node_modules
/
@parcel
/
watcher
/
src
RSK World
ruby-calculator
Ruby Calculator Pro - Interactive Calculator with 8 Types + Mathematical Functions + Rails MVC + Modern Web Interface + API Integration + Educational Design
src
  • kqueue
  • linux
  • macos
  • shared
  • unix
  • wasm
  • watchman
  • windows
  • Backend.cc4.3 KB
  • Backend.hh874 B
  • Debounce.cc2.5 KB
  • Debounce.hh883 B
  • DirTree.cc4.2 KB
  • DirTree.hh1.1 KB
  • Event.hh2.5 KB
  • Glob.cc539 B
  • Glob.hh509 B
  • PromiseRunner.hh2.5 KB
  • Signal.hh816 B
  • Watcher.cc6.2 KB
  • Watcher.hh1.8 KB
  • binding.cc6.8 KB
Debounce.cc
node_modules/@parcel/watcher/src/Debounce.cc
Raw Download
Find: Go to:
#include "Debounce.hh"

#ifdef __wasm32__
extern "C" void on_timeout(void *ctx) {
  Debounce *debounce = (Debounce *)ctx;
  debounce->notify();
}
#endif

std::shared_ptr<Debounce> Debounce::getShared() {
  static std::weak_ptr<Debounce> sharedInstance;
  std::shared_ptr<Debounce> shared = sharedInstance.lock();
  if (!shared) {
    shared = std::make_shared<Debounce>();
    sharedInstance = shared;
  }

  return shared;
}

Debounce::Debounce() {
  mRunning = true;
  #ifndef __wasm32__
    mThread = std::thread([this] () {
      loop();
    });
  #endif
}

Debounce::~Debounce() {
  mRunning = false;
  #ifndef __wasm32__
    mWaitSignal.notify();
    mThread.join();
  #endif
}

void Debounce::add(void *key, std::function<void()> cb) {
  std::unique_lock<std::mutex> lock(mMutex);
  mCallbacks.emplace(key, cb);
}

void Debounce::remove(void *key) {
  std::unique_lock<std::mutex> lock(mMutex);
  mCallbacks.erase(key);
}

void Debounce::trigger() {
  std::unique_lock<std::mutex> lock(mMutex);
  #ifdef __wasm32__
    notifyIfReady();
  #else
    mWaitSignal.notify();
  #endif
}

#ifndef __wasm32__
void Debounce::loop() {
  while (mRunning) {
    mWaitSignal.wait();
    if (!mRunning) {
      break;
    }

    notifyIfReady();
  }
}
#endif

void Debounce::notifyIfReady() {
  if (!mRunning) {
    return;
  }

  // If we haven't seen an event in more than the maximum wait time, notify callbacks immediately
  // to ensure that we don't wait forever. Otherwise, wait for the minimum wait time and batch
  // subsequent fast changes. This also means the first file change in a batch is notified immediately,
  // separately from the rest of the batch. This seems like an acceptable tradeoff if the common case
  // is that only a single file was updated at a time.
  auto time = std::chrono::steady_clock::now();
  if ((time - mLastTime) > std::chrono::milliseconds(MAX_WAIT_TIME)) {
    mLastTime = time;
    notify();
  } else {
    wait();
  }
}

void Debounce::wait() {
  #ifdef __wasm32__
    clear_timeout(mTimeout);
    mTimeout = set_timeout(MIN_WAIT_TIME, this);
  #else
    auto status = mWaitSignal.waitFor(std::chrono::milliseconds(MIN_WAIT_TIME));
    if (mRunning && (status == std::cv_status::timeout)) {
      notify();
    }
  #endif
}

void Debounce::notify() {
  std::unique_lock<std::mutex> lock(mMutex);

  mLastTime = std::chrono::steady_clock::now();
  for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
    auto cb = it->second;
    cb();
  }

  #ifndef __wasm32__
    mWaitSignal.reset();
  #endif
}
114 lines•2.5 KB
cpp

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer