1
1
mirror of https://git.shadowkat.net/izaya/OC-PsychOS2.git synced 2024-11-26 04:57:24 -05:00
OC-PsychOS2/lib/event.lua

43 lines
1.1 KiB
Lua
Raw Normal View History

local event = {}
2019-07-14 06:52:56 -04:00
function event.pull(t,...) -- return an event, optionally with timeout *t* and filter *...*.
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
if not (tEv[i] or ""):match(tA[i]) then
ret = false
end
end
if ret then return table.unpack(tEv) end
until computer.uptime() > tE
return nil
end
2019-07-14 06:52:56 -04:00
function event.listen(e,f) -- run function *f* for every occurance of event *e*
local op = os.getenv("parent")
os.setenv("parent",cPid)
os.spawn(function() while true do
local tEv = {coroutine.yield()}
if tEv[1] == e then
f(table.unpack(tEv))
end
if not tTasks[os.getenv("parent")] or (tEv[1] == "unlisten" and tEv[2] == e and tEv[3] == tostring(f)) then break end
end end,string.format("[%d] %s listener",cPid,e))
os.setenv("parent",op)
end
2019-07-14 06:52:56 -04:00
function event.ignore(e,f) -- stop function *f* running for every occurance of event *e*
computer.pushSignal("unlisten",e,tostring(f))
end
return event