moontalk/server/eventloop-server-experiment/eventloop.4th

44 lines
920 B
Plaintext
Raw Permalink Normal View History

2024-02-03 12:37:01 -05:00
require events.4th
require eventhandlers.4th
variable continue
: (call) ( eventdata-u eventid-u eventhandler-addr -- )
eventhandler.xt @ execute ;
: (handles?) ( eventid-u eventhandler-addr -- flag )
tuck eventhandler.eventid @ = swap eventhandler.xt @ 0<> and ;
: eventloop.has-events? ( -- flag )
events.has-item? ;
: eventloop.stop-propagation ( -- )
false continue ! ;
: eventloop.dispatch ( -- )
true continue !
events.dequeue
MAX_EVENTHANDLERS 0 DO
continue @ invert IF
2drop UNLOOP EXIT
THEN
2dup I (translate) 2dup (handles?) IF
(call)
ELSE
drop 2drop
THEN
LOOP 2drop ;
: eventloop.run ( -- )
BEGIN
eventloop.has-events? IF
eventloop.dispatch
ELSE
EXIT
THEN
AGAIN ;
: eventloop.idlerun ( -- )
BEGIN
eventloop.has-events? IF
eventloop.dispatch
ELSE
0 0 events.enqueue eventloop.dispatch
THEN
AGAIN ;