Skip to content

Commit c38e773

Browse files
committed
Refactored the throttled dirtyable to work with time not a counter
1 parent 0c79632 commit c38e773

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/jobs/UpdateQuotesJob.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.Objects;
1313
import java.util.Optional;
1414
import java.util.Set;
15-
import java.util.concurrent.atomic.AtomicInteger;
15+
import java.util.concurrent.atomic.AtomicBoolean;
1616
import java.util.function.Predicate;
1717
import java.util.stream.Collectors;
1818

@@ -55,33 +55,45 @@ public enum Target
5555
}
5656

5757
/**
58-
* Keeps dirty state of parallel jobs and marks the client file dirty after
59-
* 20 dirty result. Background: marking the client dirty after every job
60-
* sends too many update events to the GUI.
58+
* Throttles the dirty notification to the user interface. Background:
59+
* marking the client dirty after every job sends too many update events to
60+
* the user interface.
6161
*/
6262
private static class Dirtyable
6363
{
64-
private static final int THRESHOLD = 20;
64+
private static final long TIME_THRESHOLD_MS = 250;
6565

6666
private final Client client;
67-
private AtomicInteger counter;
67+
private final AtomicBoolean dirtyState = new AtomicBoolean(false);
68+
private long lastUpdate = System.currentTimeMillis();
6869

6970
public Dirtyable(Client client)
7071
{
7172
this.client = client;
72-
this.counter = new AtomicInteger();
7373
}
7474

75-
public void markDirty()
75+
public synchronized void markDirty()
7676
{
77-
int count = counter.incrementAndGet();
78-
if (count % THRESHOLD == 0)
77+
long now = System.currentTimeMillis();
78+
if (now - lastUpdate >= TIME_THRESHOLD_MS)
79+
{
7980
client.markDirty();
81+
dirtyState.set(false);
82+
lastUpdate = now;
83+
}
84+
else
85+
{
86+
dirtyState.set(true);
87+
}
8088
}
8189

82-
public boolean isDirty()
90+
public synchronized void flushIfDue()
8391
{
84-
return counter.get() % THRESHOLD != 0;
92+
if (dirtyState.get())
93+
{
94+
client.markDirty();
95+
dirtyState.set(false);
96+
}
8597
}
8698
}
8799

@@ -248,8 +260,7 @@ protected IStatus run(IProgressMonitor monitor)
248260
if (!jobs.isEmpty())
249261
runJobs(monitor, jobs);
250262

251-
if (!monitor.isCanceled() && dirtyable.isDirty())
252-
getClient().markDirty();
263+
dirtyable.flushIfDue();
253264

254265
if (repeatPeriod > 0)
255266
schedule(repeatPeriod);

0 commit comments

Comments
 (0)