BananaOS/test/lib/event.lua

44 lines
1.2 KiB
Lua
Raw Normal View History

2025-02-21 00:33:21 -05:00
_G.event = {}
local computer = require("computer")
function event.pull(name, timeout)
local rawSignal = {}
local signals_recieved_in_mean_time = {}
local timeouts = 0
if not timeout then
timeout = 99999999999
end
while rawSignal[1] ~= name do
-- pull any signal
rawSignal = table.pack(computer.pullSignal(0.1))
-- if no signal then add to time timeout
if rawSignal[1] == nil then
timeouts = timeouts + 0.1
else
if rawSignal[1] ~= name then
-- not the signal, push to the signals we got in the mean time
table.insert(signals_recieved_in_mean_time,rawSignal)
else
-- got the signal
break
end
end
if timeouts >= timeout then
-- we never got the signal :(
return nil
end
end
-- rethrow all signals... they weren't what we wanted
if #signals_recieved_in_mean_time > 0 then
for i in ipairs(signals_recieved_in_mean_time) do
computer.pushSignal(table.unpack(signals_recieved_in_mean_time[i]))
end
end
return table.unpack(rawSignal)
end
return event