lib: improve new block debouncer

This commit is contained in:
Oscar Mira 2023-04-20 18:53:05 +02:00
parent 46d387ae37
commit 8645d86d3e

View File

@ -136,11 +136,21 @@ void Wallet::handleBalanceChanged(uint64_t at_block_height) {
void Wallet::handleNewBlock(uint64_t height, bool debounce) {
m_blockchain_height = height;
bool notify = false;
if (debounce) {
// Notify the blockchain height once every 200 ms if the height is a multiple of 100.
if (height % 100 == 0) {
static std::chrono::steady_clock::time_point last_time;
auto now = std::chrono::steady_clock::now();
// Notify blockchain height every one second.
if (!debounce || (now - last_time >= 1.s)) {
if (now - last_time >= 200.ms) {
last_time = now;
notify = true;
}
}
} else {
notify = true;
}
if (notify) {
m_callback.callVoidMethod(getJniEnv(), WalletNative_onRefresh, height, false);
}
}