Skip to content

Commit 0f41f29

Browse files
committed
added EventLoop.addAsync(), event.start()
1 parent 6daed43 commit 0f41f29

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

std/haxe/EventLoop.hx

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ class Event {
1919
var toRemove : Bool;
2020
var nextRun : Float = Math.NEGATIVE_INFINITY;
2121

22-
function new(events, callb, p) {
22+
function new(events, p) {
2323
this.events = events;
24-
this.callb = callb;
2524
this.priority = p;
2625
}
2726

@@ -37,7 +36,22 @@ class Event {
3736
Stop this event from repeating.
3837
**/
3938
public function stop() {
40-
@:privateAccess events.remove(this);
39+
@:privateAccess events.stop(this);
40+
}
41+
42+
/**
43+
Stop this event from repeating.
44+
**/
45+
public function start( callb : Void -> Void ) {
46+
this.callb = callb;
47+
@:privateAccess events.start(this);
48+
}
49+
50+
/**
51+
Tells if the event has been stopped.
52+
**/
53+
public function isStopped() {
54+
return toRemove || (prev == null && @:privateAccess events.events != this);
4155
}
4256

4357
}
@@ -142,7 +156,7 @@ class EventLoop {
142156
var e = events;
143157
while( e != null ) {
144158
var n = e.next;
145-
if( e.toRemove ) remove(e);
159+
if( e.toRemove ) stop(e);
146160
e = n;
147161
}
148162
}
@@ -153,31 +167,27 @@ class EventLoop {
153167
Add a callback to be run at each loop of the event loop.
154168
**/
155169
public function add( callb : Void -> Void, priority = 0 ) : Event {
156-
var e = new Event(this,callb,priority);
157-
lock();
158-
if( events != null )
159-
events.prev = e;
160-
e.next = events;
161-
events = e;
162-
wakeup();
163-
unlock();
170+
var e = new Event(this,priority);
171+
e.start(callb);
164172
return e;
165173
}
166174

175+
/**
176+
Similar to `add` but will return the Event before it's started.
177+
This is useful if you wish to hold a reference of another thread Event loop
178+
before it runs.
179+
**/
180+
public function addAsync( priority = 0 ) {
181+
return new Event(this,priority);
182+
}
183+
167184
/**
168185
Add a callback to be run every `delay` seconds until stopped
169186
**/
170187
public function addTimer( callb : Void -> Void, delay : Float, priority = 0 ) : Event {
171-
var e : Event = null;
172-
e = new Event(this,function() { e.delay(delay,true); callb(); },priority);
188+
var e : Event = new Event(this,priority);
173189
e.delay(delay);
174-
lock();
175-
if( events != null )
176-
events.prev = e;
177-
e.next = events;
178-
events = e;
179-
wakeup();
180-
unlock();
190+
e.start(function() { e.delay(delay,true); callb(); });
181191
return e;
182192
}
183193

@@ -198,7 +208,22 @@ class EventLoop {
198208
return e;
199209
}
200210

201-
function remove( e : Event ) {
211+
function start( e : Event ) {
212+
lock();
213+
e.toRemove = false;
214+
if( !e.isStopped() ) {
215+
unlock();
216+
return;
217+
}
218+
if( events != null )
219+
events.prev = e;
220+
e.next = events;
221+
events = e;
222+
wakeup();
223+
unlock();
224+
}
225+
226+
function stop( e : Event ) {
202227
lock();
203228
if( inLoop ) {
204229
// prevent remove while in loopOnce()
@@ -207,6 +232,11 @@ class EventLoop {
207232
unlock();
208233
return;
209234
}
235+
e.toRemove = false;
236+
if( e.isStopped() ) {
237+
unlock();
238+
return;
239+
}
210240
if( events == e )
211241
events = e.next;
212242
else if( e.prev != null )

0 commit comments

Comments
 (0)