@@ -12,7 +12,6 @@ local expect = require("libraries/expect")
1212--- @field traceback boolean Whether to show a traceback on errors
1313--- @field _events table A table of events and their callbacks
1414--- @field _schedule function[] A table of scheduled functions
15- --- @field _eventQueue table A table of unfinished events
1615--- @field _plugins table A table of plugins
1716--- @field isRunning boolean Whether the Basalt runtime is active
1817--- @field LOGGER Log The logger instance
@@ -21,7 +20,6 @@ local basalt = {}
2120basalt .traceback = true
2221basalt ._events = {}
2322basalt ._schedule = {}
24- basalt ._eventQueue = {}
2523basalt ._plugins = {}
2624basalt .isRunning = false
2725basalt .LOGGER = require (" log" )
@@ -245,50 +243,27 @@ local function updateEvent(event, ...)
245243 end
246244 end
247245
248- -- Main event coroutine system
249- for k ,v in pairs (basalt ._eventQueue ) do
250- if coroutine.status (v .coroutine ) == " suspended" then
251- if v .filter == event or v .filter == nil then
252- v .filter = nil
253- local ok , result = coroutine.resume (v .coroutine , event , ... )
254- if not ok then
255- errorManager .header = " Basalt Event Error"
256- errorManager .error (result )
257- end
258- v .filter = result
259- end
260- end
261- if coroutine.status (v .coroutine ) == " dead" then
262- table.remove (basalt ._eventQueue , k )
263- end
264- end
246+ -- Dispatch the current event directly in the main loop. Handlers must run synchronously
247+ -- and must not yield; blocking code (sleep/read/pullEvent) has to be wrapped by the user
248+ -- in basalt.schedule(...).
249+ basaltEvent ()
265250
266- local newEvent = {coroutine = coroutine.create (basaltEvent ), filter = event }
267- local ok , result = coroutine.resume (newEvent .coroutine , event , ... )
268- if (not ok )then
269- errorManager .header = " Basalt Event Error"
270- errorManager .error (result )
271- end
272- if (result ~= nil )then
273- newEvent .filter = result
274- end
275- table.insert (basalt ._eventQueue , newEvent )
276-
277- -- Schedule event coroutine system
278- for _ , func in ipairs (basalt ._schedule ) do
279- if coroutine.status (func .coroutine )== " suspended" then
280- if event == func .filter or func .filter == nil then
281- func .filter = nil
251+ -- Schedule coroutine system: drive basalt.schedule() coroutines with this event.
252+ -- Iterate backwards so in-place removal stays safe.
253+ for i = # basalt ._schedule , 1 , - 1 do
254+ local func = basalt ._schedule [i ]
255+ if coroutine.status (func .coroutine ) == " suspended" then
256+ if func .filter == nil or func .filter == event then
282257 local ok , result = coroutine.resume (func .coroutine , event , ... )
283- if ( not ok ) then
258+ if not ok then
284259 errorManager .header = " Basalt Schedule Error"
285260 errorManager .error (result )
286261 end
287262 func .filter = result
288263 end
289264 end
290- if ( coroutine.status (func .coroutine )== " dead" ) then
291- basalt . removeSchedule ( func . coroutine )
265+ if coroutine.status (func .coroutine ) == " dead" then
266+ table.remove ( basalt . _schedule , i )
292267 end
293268 end
294269
0 commit comments