Skip to content

Commit c2635c2

Browse files
Convert timers to seconds and add tests
Change timer semantics and add unit tests. - Convert timer time units from milliseconds to seconds across the API and docs (Timer, TimerScheduler, and related code) to use seconds consistently. - Make TimerNode.pausedAt nullable and update pause/resume logic in TimerHeap to handle null, adjust queue operations (enqueue instead of update) and default clear(handle) immediate parameter to true. - Update Timer.current thread/non-threaded checks and error handling. - Replace the haxe.Timer implementation with a CrossByte tick-backed variant (start/stop behavior, stamp handling) and guard against double-stop cleanup. - Add tests: HaxeTimerTest, TimerTestMain, and new test cases in TimerHeapTest; wire HaxeTimerTest into tests/TestMain. - Remove a dev haxelib crossbyte line from CI workflow. Files changed include: .github/workflows/ci.yml, multiple src/crossbyte timer files, src/haxe/Timer.hx, and several new/updated tests under tests/crossbyte/timer.
1 parent 263c8c1 commit c2635c2

11 files changed

Lines changed: 115 additions & 70 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ jobs:
138138
run: |
139139
haxelib install utest --quiet
140140
haxelib install hxcpp --quiet
141-
haxelib dev crossbyte ./crossbyte
142141
haxelib dev crossbyte-libuv ./crossbyte-libuv
143142
144143
- name: Run extension non-native tests

‎src/crossbyte/Timer.hx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ class Timer {
4242
@:noCompletion private static inline function current():TimerScheduler {
4343
#if cpp
4444
final scheduler:TimerScheduler = __tls.value;
45-
#if debug
4645
if (scheduler == null) {
4746
throw "TimerScheduler not attached to this thread";
4847
}
49-
#end
5048
return scheduler;
5149
#else
50+
if (__nonThreadedTimer == null) {
51+
throw "TimerScheduler not attached to this thread";
52+
}
5253
return __nonThreadedTimer;
5354
#end
5455
}
@@ -179,7 +180,7 @@ class Timer {
179180
}
180181

181182
/**
182-
* Converts an absolute wall clock time (in milliseconds since epoch)
183+
* Converts an absolute wall clock time (in seconds)
183184
* to the scheduler's virtual time.
184185
*
185186
* @param wallTime The absolute wall clock time.
@@ -194,7 +195,7 @@ class Timer {
194195
* Converts a scheduler virtual time back into a wall clock timestamp.
195196
*
196197
* @param virtualTime The virtual time from the scheduler.
197-
* @return The corresponding wall clock time in milliseconds since epoch.
198+
* @return The corresponding wall clock time in seconds.
198199
*/
199200
public static inline function toWallClock(virtualTime:Float):Float {
200201
final scheduler = current();

‎src/crossbyte/_internal/system/timer/ITimerScheduler.hx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface ITimerScheduler {
88
public function setTimeoutVoid(delay:Float, callback:Void->Void):TimerHandle;
99
public function setInterval(delay:Float, interval:Float, callback:TimerHandle->Void):TimerHandle;
1010
public function setIntervalVoid(delay:Float, interval:Float, callback:Void->Void):TimerHandle;
11-
public function clear(handle:TimerHandle, immediate:Bool = false):Bool;
11+
public function clear(handle:TimerHandle, immediate:Bool = true):Bool;
1212
public function isActive(handle:TimerHandle):Bool;
1313
public function schedule(time:Float, callback:TimerHandle->Void):TimerHandle;
1414
public function scheduleVoid(time:Float, callback:Void->Void):TimerHandle;

‎src/crossbyte/_internal/system/timer/TimerNode.hx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TimerNode {
66
public var time:Float;
77
public var interval:Float;
88
public var enabled:Bool = true;
9-
public var pausedAt:Float = 0.0;
9+
public var pausedAt:Null<Float> = null;
1010
public var callback:TimerHandle->Void;
1111

1212
public inline function new(id:Int, time:Float, interval:Float, callback:TimerHandle->Void) {
@@ -15,4 +15,4 @@ class TimerNode {
1515
this.interval = interval;
1616
this.callback = callback;
1717
}
18-
}
18+
}

‎src/crossbyte/_internal/system/timer/TimerScheduler.hx‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ abstract TimerScheduler(ITimerScheduler) from ITimerScheduler to ITimerScheduler
6161
}
6262

6363
/**
64-
* Schedules a one-shot timer to fire after a delay (in milliseconds).
64+
* Schedules a one-shot timer to fire after a delay (in seconds).
6565
* The callback receives the actual fire time.
6666
*
67-
* @param delay Milliseconds from now to fire the timer.
67+
* @param delay Seconds from now to fire the timer.
6868
* @param callback A function that receives the current time when the timer fires.
6969
* @return A handle used to manage the timer.
7070
*/
@@ -76,8 +76,8 @@ abstract TimerScheduler(ITimerScheduler) from ITimerScheduler to ITimerScheduler
7676
* Schedules a one-shot timer from a specific start time.
7777
* The callback does not receive any parameters.
7878
*
79-
* @param startTime Start time in ms.
80-
* @param delay Milliseconds after start time to fire.
79+
* @param startTime Start time in seconds.
80+
* @param delay Seconds after start time to fire.
8181
* @param callback A function to invoke when the timer fires.
8282
* @return A handle used to manage the timer.
8383
*/
@@ -89,8 +89,8 @@ abstract TimerScheduler(ITimerScheduler) from ITimerScheduler to ITimerScheduler
8989
* Schedules a repeating timer starting at a given time and repeating
9090
* at the specified interval. The callback receives the current time.
9191
*
92-
* @param startTime When the first fire should occur (in ms).
93-
* @param delay How often to repeat (in ms).
92+
* @param startTime When the first fire should occur (in seconds).
93+
* @param delay How often to repeat (in seconds).
9494
* @param callback A function receiving the current time each fire.
9595
* @return A handle used to manage the timer.
9696
*/
@@ -101,8 +101,8 @@ abstract TimerScheduler(ITimerScheduler) from ITimerScheduler to ITimerScheduler
101101
/**
102102
* Schedules a repeating timer without parameters.
103103
*
104-
* @param startTime When to start (in ms).
105-
* @param delay Interval duration (in ms).
104+
* @param startTime When to start (in seconds).
105+
* @param delay Interval duration (in seconds).
106106
* @param callback A function to invoke each interval.
107107
* @return A handle used to manage the timer.
108108
*/
@@ -160,7 +160,7 @@ abstract TimerScheduler(ITimerScheduler) from ITimerScheduler to ITimerScheduler
160160
* Reschedules a timer to fire at a new time.
161161
*
162162
* @param handle The timer handle to modify.
163-
* @param time The new time (in ms) to fire.
163+
* @param time The new time (in seconds) to fire.
164164
* @return `true` if rescheduled successfully.
165165
*/
166166
public function reschedule(handle:Int, time:Float):Bool {
@@ -194,7 +194,7 @@ abstract TimerScheduler(ITimerScheduler) from ITimerScheduler to ITimerScheduler
194194
* Resumes a paused timer.
195195
*
196196
* @param handle The timer handle.
197-
* @param time Optional resume time (in ms).
197+
* @param time Optional resume time (in seconds).
198198
* @param policy Optional resume behavior policy.
199199
* @return `true` if resumed.
200200
*/
@@ -203,7 +203,7 @@ abstract TimerScheduler(ITimerScheduler) from ITimerScheduler to ITimerScheduler
203203
}
204204

205205
/**
206-
* Returns the next due time (in ms) for the earliest scheduled timer,
206+
* Returns the next due time (in seconds) for the earliest scheduled timer,
207207
* or `null` if the queue is empty.
208208
*/
209209
public inline function nextDue():Null<Float> {
@@ -213,7 +213,7 @@ abstract TimerScheduler(ITimerScheduler) from ITimerScheduler to ITimerScheduler
213213
/**
214214
* Processes and fires any timers due at or before the given time.
215215
*
216-
* @param time The current time in ms.
216+
* @param time The current time delta in seconds.
217217
* @param maxFires Optional limit on how many timers to fire (default 256).
218218
* @return The number of timers fired.
219219
*/

‎src/crossbyte/_internal/system/timer/heap/TimerHeap.hx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class TimerHeap implements ITimerScheduler {
6464
return createTimer(__now + delay, interval, (handle:TimerHandle) -> callback());
6565
}
6666

67-
public function clear(handle:TimerHandle, immediate:Bool = false):Bool {
67+
public function clear(handle:TimerHandle, immediate:Bool = true):Bool {
6868
if (!isLive(handle)) {
6969
return false;
7070
}
@@ -141,19 +141,19 @@ class TimerHeap implements ITimerScheduler {
141141
node.enabled = true;
142142
switch (policy) {
143143
case KeepPhase:
144-
if (node.pausedAt != 0.0) {
144+
if (node.pausedAt != null) {
145145
var pausedDur = t - node.pausedAt;
146146
if (pausedDur != 0.0) {
147147
node.time += pausedDur;
148148
}
149149

150-
node.pausedAt = 0.0;
150+
node.pausedAt = null;
151151
}
152152
case FromNow:
153153
node.time = (node.interval > 0) ? (t + node.interval) : t;
154-
node.pausedAt = 0.0;
154+
node.pausedAt = null;
155155
}
156-
queue.update(node);
156+
queue.enqueue(node);
157157
return true;
158158
}
159159

‎src/haxe/Timer.hx‎

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,30 @@
11
package haxe;
22

3-
#if lime_cffi
4-
import lime.system.System;
5-
#else
63
import crossbyte.core.CrossByte;
74
import crossbyte.events.TickEvent;
85
import haxe.ds.IntMap;
9-
#end
106
import haxe.Log;
117
import haxe.PosInfos;
128

139
/**
14-
A hybrid `haxe.Timer` implementation that preserves CrossByte's tick-driven
15-
behavior in standalone CrossByte runtimes while remaining compatible with
16-
Lime-based hosts that expect the legacy `lime_cffi` timer surface.
10+
A `haxe.Timer` implementation backed by CrossByte's tick-driven runtime.
1711
**/
1812
class Timer {
19-
#if lime_cffi
20-
private static var sRunningTimers:Array<Timer> = [];
21-
22-
private var mTime:Float;
23-
private var mFireAt:Float;
24-
private var mRunning:Bool;
25-
26-
public function new(time:Float) {
27-
mTime = time;
28-
sRunningTimers.push(this);
29-
mFireAt = getMS() + mTime;
30-
mRunning = true;
31-
}
32-
33-
private static inline function getMS():Float {
34-
return System.getTimer();
35-
}
36-
37-
public function stop():Void {
38-
mRunning = false;
39-
}
40-
41-
@:noCompletion private function __check(inTime:Float):Void {
42-
if (inTime >= mFireAt) {
43-
mFireAt += mTime;
44-
run();
45-
}
46-
}
47-
#else
4813
private static var timerCount:Int = 0;
4914
private static var timers:IntMap<Timer> = new IntMap<Timer>();
5015
private static var __currentId:Int = 0;
5116

5217
private var delayCount:Float;
5318
private var timeRemaining:Float;
5419
private var running:Bool;
20+
private var stopped:Bool;
5521
private var id:Int;
5622

5723
public function new(time_ms:Int) {
5824
delayCount = time_ms / 1000;
5925
timeRemaining = delayCount;
60-
running = false;
26+
running = true;
27+
stopped = false;
6128
id = ++__currentId;
6229
timerCount++;
6330

@@ -87,21 +54,33 @@ class Timer {
8754
}
8855

8956
private function cleanup():Void {
57+
if (stopped) {
58+
return;
59+
}
60+
61+
stopped = true;
9062
timers.remove(id);
9163
if (--timerCount == 0) {
9264
CrossByte.current().removeEventListener(TickEvent.TICK, onTick);
9365
}
9466
}
9567

9668
public function stop():Void {
69+
if (stopped) {
70+
return;
71+
}
72+
9773
running = false;
9874
cleanup();
9975
}
10076

10177
public function start():Void {
78+
if (stopped) {
79+
return;
80+
}
81+
10282
running = true;
10383
}
104-
#end
10584

10685
public dynamic function run():Void {}
10786

@@ -111,9 +90,6 @@ class Timer {
11190
timer.stop();
11291
f();
11392
};
114-
#if !lime_cffi
115-
timer.start();
116-
#end
11793
return timer;
11894
}
11995

@@ -125,10 +101,7 @@ class Timer {
125101
}
126102

127103
public static inline function stamp():Float {
128-
#if lime_cffi
129-
var timer = System.getTimer();
130-
return (timer > 0 ? timer / 1000 : 0);
131-
#elseif js
104+
#if js
132105
return Date.now().getTime() / 1000;
133106
#elseif cpp
134107
return untyped __global__.__time_stamp();

‎tests/TestMain.hx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TestMain {
3434
#end
3535
runner.addCase(new crossbyte.net.WebSocketTest());
3636
runner.addCase(new crossbyte.resources.ResourcesTest());
37+
runner.addCase(new crossbyte.timer.HaxeTimerTest());
3738
runner.addCase(new crossbyte.timer.TimerHeapTest());
3839
Report.create(runner);
3940
runner.run();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package crossbyte.timer;
2+
3+
import haxe.Timer as HxTimer;
4+
import utest.Assert;
5+
6+
@:access(haxe.Timer)
7+
class HaxeTimerTest extends utest.Test {
8+
public function testConstructorStartsTimerImmediately():Void {
9+
var fired = 0;
10+
var timer = new HxTimer(100);
11+
timer.run = function() fired++;
12+
13+
@:privateAccess timer.__update(0.05);
14+
Assert.equals(0, fired);
15+
16+
@:privateAccess timer.__update(0.05);
17+
Assert.equals(1, fired);
18+
19+
timer.stop();
20+
}
21+
22+
public function testStopIsIdempotent():Void {
23+
var baseCount = @:privateAccess HxTimer.timerCount;
24+
var timer = new HxTimer(100);
25+
26+
Assert.equals(baseCount + 1, @:privateAccess HxTimer.timerCount);
27+
28+
timer.stop();
29+
Assert.equals(baseCount, @:privateAccess HxTimer.timerCount);
30+
31+
timer.stop();
32+
Assert.equals(baseCount, @:privateAccess HxTimer.timerCount);
33+
}
34+
}

‎tests/crossbyte/timer/TimerHeapTest.hx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package crossbyte.timer;
22

3+
import crossbyte._internal.system.timer.ResumePolicy;
34
import crossbyte._internal.system.timer.heap.TimerHeap;
45
import utest.Assert;
56

@@ -23,6 +24,9 @@ class TimerHeapTest extends utest.Test {
2324
var handle = heap.setTimeout(1.0, _ -> fired = true);
2425

2526
Assert.isTrue(heap.clear(handle));
27+
Assert.isTrue(heap.isEmpty);
28+
Assert.equals(0, heap.size);
29+
Assert.equals(null, heap.nextDue());
2630
Assert.equals(0, heap.advanceTime(1.0));
2731
Assert.isFalse(fired);
2832
}
@@ -37,4 +41,21 @@ class TimerHeapTest extends utest.Test {
3741
Assert.equals(2, fired);
3842
Assert.isFalse(heap.isEmpty);
3943
}
44+
45+
public function testPauseResumeKeepPhaseFromZeroPreservesRemainingDelay():Void {
46+
var heap = new TimerHeap();
47+
var fired = 0;
48+
var handle = heap.setTimeout(1.0, _ -> fired++);
49+
50+
Assert.isTrue(heap.setEnabled(handle, false));
51+
Assert.equals(0, heap.advanceTime(0.5));
52+
Assert.equals(0, fired);
53+
54+
Assert.isTrue(heap.setEnabled(handle, true, ResumePolicy.KeepPhase, heap.time));
55+
Assert.equals(0, heap.advanceTime(0.99));
56+
Assert.equals(0, fired);
57+
58+
Assert.equals(1, heap.advanceTime(0.01));
59+
Assert.equals(1, fired);
60+
}
4061
}

0 commit comments

Comments
 (0)