2019-01-08 02:10:22 -05:00
|
|
|
local event = {}
|
2020-05-11 20:57:13 -04:00
|
|
|
function event.pull(t,...) -- number -- -- return an event, optionally with timeout *t* and filter *...*.
|
2019-01-08 02:10:22 -05:00
|
|
|
local tA = {...}
|
|
|
|
if type(t) == "string" then
|
|
|
|
table.insert(tA,1,t)
|
|
|
|
t = 0
|
|
|
|
end
|
|
|
|
if not t or t <= 0 then
|
|
|
|
t = math.huge
|
|
|
|
end
|
|
|
|
local tE = computer.uptime()+t
|
|
|
|
repeat
|
|
|
|
tEv = {coroutine.yield()}
|
|
|
|
local ret = true
|
|
|
|
for i = 1, #tA do
|
2019-11-26 02:50:36 -05:00
|
|
|
if type(tEv[i]) == "string" and type(tA[i]) == "string" then
|
|
|
|
if not (tEv[i] or ""):match(tA[i]) then
|
|
|
|
ret = false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
ret = tEv[i] == tA[i]
|
2019-01-08 02:10:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if ret then return table.unpack(tEv) end
|
|
|
|
until computer.uptime() > tE
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2020-05-11 20:57:13 -04:00
|
|
|
function event.listen(e,f) -- string function -- -- run function *f* for every occurance of event *e*
|
2019-01-08 02:10:22 -05:00
|
|
|
os.spawn(function() while true do
|
|
|
|
local tEv = {coroutine.yield()}
|
|
|
|
if tEv[1] == e then
|
|
|
|
f(table.unpack(tEv))
|
|
|
|
end
|
2019-11-09 00:09:23 -05:00
|
|
|
if not os.taskInfo(os.taskInfo().parent) or (tEv[1] == "unlisten" and tEv[2] == e and tEv[3] == tostring(f)) then break end
|
|
|
|
end end,string.format("[%d] %s listener",os.pid(),e))
|
2019-01-08 02:10:22 -05:00
|
|
|
end
|
|
|
|
|
2020-05-11 21:44:05 -04:00
|
|
|
function event.ignore(e,f) -- string function -- -- stop function *f* running for every occurance of event *e*
|
2019-01-08 02:10:22 -05:00
|
|
|
computer.pushSignal("unlisten",e,tostring(f))
|
|
|
|
end
|
|
|
|
|
|
|
|
return event
|