// only executes one action at once. keeps track of // optimal timestamps though, so actions may pile up sclass FlexibleRateTimer implements AutoCloseable { double hertz; transient Runnable action; transient double timestamp; transient DoLater doLater; transient new Flag started; transient volatile bool disposed; transient long delay; // how much are we currently delayed transient L onFrequencyChanged; static double maxHertz = 1000; *() {} *(double *hertz) {} *(double *hertz, Runnable *action) {} void start { if (!started.raise()) ret; _kaboom(); } synchronized void _kaboom { if (disposed) ret; dispose doLater; if (timestamp == 0) timestamp = sysNow(); timestamp += 1000/getFrequency(); long _timestamp = lround(timestamp); doLater = new DoLater(_timestamp, r { delay = sysNow()-_timestamp; pcallF(action); _kaboom(); }); doLater.enable(); } void cancel { close(); } public void close { set disposed; dispose doLater; } void setRunnableAndStart(Runnable action) { this.action = action; start(); } // takes affect _after_ next trigger synchronized void setFrequency(double hertz) { hertz = clamp(hertz, 0, maxHertz); if (hertz == this.hertz) ret; this.hertz = hertz; pcallFAll(onFrequencyChanged); } // reschedule next action synchronized void setFrequencyImmediately(double hertz) { hertz = clamp(hertz, 0, maxHertz); if (hertz == this.hertz) ret; if (!started.isUp()) ret; if (!doLater.cancel()) ret; timestamp -= 1000/getFrequency(); this.hertz = hertz; pcallFAll(onFrequencyChanged); _kaboom(); } synchronized double getFrequency() { ret hertz; } long currentDelay() { ret delay; } synchronized void onFrequencyChanged(Runnable r) { onFrequencyChanged = addOrCreate(onFrequencyChanged, r); } }