entirely rework event handling take 3

This commit is contained in:
2025-12-29 01:19:07 -05:00
parent 74dd72f478
commit c5413a378b
3 changed files with 50 additions and 42 deletions

View File

@ -13,14 +13,14 @@ local handlerPriorities = {}
-- PRIORITY LEVELS (higher = more priority)
---------------------------------------------
events.PRIORITY = {
PASSWORD_INPUT = 100, -- Highest priority - password entry
ENTITY_READ = 50, -- Entity tracking
MESSAGE = 50, -- Message handling
PASSWORD_INPUT = 100, -- Highest priority - password entry
ENTITY_READ = 50, -- Entity tracking
MESSAGE = 50, -- Message handling
DISCONNECT_BUTTON = 30, -- Manual disconnect button
ACTIVATION = 20, -- Incoming wormhole
DISCONNECT_CHECK = 10, -- Natural disconnection
TIMEOUT = 5, -- Timeout handlers
DEFAULT = 1 -- Default priority
ACTIVATION = 20, -- Incoming wormhole
DISCONNECT_CHECK = 10, -- Natural disconnection
TIMEOUT = 5, -- Timeout handlers
DEFAULT = 1 -- Default priority
}
---------------------------------------------
@ -29,12 +29,12 @@ events.PRIORITY = {
function events.registerHandler(eventType, callback, priority)
priority = priority or events.PRIORITY.DEFAULT
if not eventHandlers[eventType] then
eventHandlers[eventType] = {}
handlerPriorities[eventType] = {}
end
table.insert(eventHandlers[eventType], callback)
table.insert(handlerPriorities[eventType], priority)
end
@ -58,7 +58,7 @@ function events.dispatch(eventType, ...)
if not handlers or #handlers == 0 then
return nil
end
-- Sort handlers by priority (highest first)
local sortedHandlers = {}
for i = 1, #handlers do
@ -67,11 +67,11 @@ function events.dispatch(eventType, ...)
priority = handlerPriorities[eventType][i]
})
end
table.sort(sortedHandlers, function(a, b)
return a.priority > b.priority
end)
-- Call handlers in priority order until one handles the event
for _, handler in ipairs(sortedHandlers) do
local result = handler.callback(...)
@ -79,7 +79,7 @@ function events.dispatch(eventType, ...)
return result
end
end
return nil
end
@ -89,11 +89,11 @@ end
function events.pullEvent(filter)
while true do
local eventData = {os.pullEvent(filter)}
local eventData = { os.pullEvent(filter) }
local eventType = eventData[1]
local result = events.dispatch(eventType, table.unpack(eventData))
-- If a handler processed the event and returned something, return it
if result ~= nil then
return result, eventType, eventData
@ -102,7 +102,7 @@ function events.pullEvent(filter)
end
function events.waitForAny(...)
local functions = {...}
local functions = { ... }
return parallel.waitForAny(table.unpack(functions))
end