blob: 018df573274a77f21f93882f1c33cf367ae3c671 (
plain)
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
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client.ui;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.Timer;
/**
* Executes the given command {@code delayMs} milliseconds after a call to
* {@link #trigger()}. Calling {@link #trigger()} again before the command has
* been executed causes the execution to be rescheduled to {@code delayMs} after
* the second call.
*
*/
public class VLazyExecutor {
private Timer timer;
private int delayMs;
private ScheduledCommand cmd;
/**
* @param delayMs
* Delay in milliseconds to wait before executing the command
* @param cmd
* The command to execute
*/
public VLazyExecutor(int delayMs, ScheduledCommand cmd) {
this.delayMs = delayMs;
this.cmd = cmd;
}
/**
* Triggers execution of the command. Each call reschedules any existing
* execution to {@link #delayMs} milliseconds from that point in time.
*/
public void trigger() {
if (timer == null) {
timer = new Timer() {
@Override
public void run() {
timer = null;
cmd.execute();
}
};
}
// Schedule automatically cancels any old schedule
timer.schedule(delayMs);
}
}
|