mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-23 15:00:30 +01:00
Refactor: Add handler, make onMissedSecondTick default
This commit is contained in:
parent
26ffe14778
commit
dcf40bcfb5
5 changed files with 21 additions and 22 deletions
|
@ -21,17 +21,24 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
// Helps configure listener objects that are run by the `UserThread` each second
|
||||
// and can do per second, per minute and delayed second actions.
|
||||
@Slf4j
|
||||
public class Clock {
|
||||
public static final int IDLE_TOLERANCE = 20000;
|
||||
public static final int IDLE_TOLERANCE_MS = 20000;
|
||||
|
||||
public interface Listener {
|
||||
void onSecondTick();
|
||||
|
||||
void onMinuteTick();
|
||||
|
||||
void onMissedSecondTick(long missed);
|
||||
default void onMissedSecondTick(long missedMs) {
|
||||
}
|
||||
|
||||
default void onAwakeFromStandby(long missedMs) {
|
||||
}
|
||||
}
|
||||
|
||||
private Timer timer;
|
||||
|
@ -55,9 +62,15 @@ public class Clock {
|
|||
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
long diff = currentTimeMillis - lastSecondTick;
|
||||
if (diff > 1000)
|
||||
listeners.forEach(listener -> listener.onMissedSecondTick(diff - 1000));
|
||||
if (diff > 1000) {
|
||||
long missedMs = diff - 1000;
|
||||
listeners.forEach(listener -> listener.onMissedSecondTick(missedMs));
|
||||
|
||||
if (missedMs > Clock.IDLE_TOLERANCE_MS) {
|
||||
log.info("We have been in standby mode for {} sec", missedMs / 1000);
|
||||
listeners.forEach(listener -> listener.onAwakeFromStandby(missedMs));
|
||||
}
|
||||
}
|
||||
lastSecondTick = currentTimeMillis;
|
||||
}, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
|
|
@ -636,10 +636,6 @@ public class TradeManager implements PersistedDataHost {
|
|||
public void onMinuteTick() {
|
||||
updateTradePeriodState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMissedSecondTick(long missed) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -149,10 +149,6 @@ public abstract class TradeStepView extends AnchorPane {
|
|||
public void onMinuteTick() {
|
||||
updateTimeLeft();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMissedSecondTick(long missed) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -81,10 +81,6 @@ public class P2pNetworkListItem {
|
|||
@Override
|
||||
public void onMinuteTick() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMissedSecondTick(long missed) {
|
||||
}
|
||||
};
|
||||
clock.addListener(listener);
|
||||
onLastActivityChanged(statistic.getLastActivityTimestamp());
|
||||
|
|
|
@ -151,12 +151,10 @@ public class PeerManager implements ConnectionListener, PersistedDataHost {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onMissedSecondTick(long missed) {
|
||||
if (missed > Clock.IDLE_TOLERANCE) {
|
||||
log.info("We have been in standby mode for {} sec", missed / 1000);
|
||||
stopped = false;
|
||||
listeners.stream().forEach(Listener::onAwakeFromStandby);
|
||||
}
|
||||
public void onAwakeFromStandby(long missedMs) {
|
||||
// TODO is "stopped = false;" correct?
|
||||
stopped = false;
|
||||
listeners.forEach(Listener::onAwakeFromStandby);
|
||||
}
|
||||
};
|
||||
clock.addListener(listener);
|
||||
|
|
Loading…
Add table
Reference in a new issue