路泰科技体检小程序UI设计新版本
qx
2025-08-06 fe97f78b9a343ee9fa45a3531d03d73dcd1df31b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#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
}