OpenComputerScripts/libevent.lua

46 lines
1.2 KiB
Lua
Raw Normal View History

2017-10-23 18:59:47 +08:00
require("checkarg")
2017-10-23 18:38:37 +08:00
local event=require("event")
2017-10-23 18:59:47 +08:00
function AddEventListener(EventName,CallbackFunction)
checkstring(EventName)
checkfunction(CallbackFunction)
return event.listen(EventName,CallbackFunction)
2017-10-23 18:38:37 +08:00
end
function RemoveEventListener(ListenerID)
2017-10-23 18:59:47 +08:00
checknumber(ListenerID)
2017-10-23 18:38:37 +08:00
return event.ignore(event.handlers[ListenerID].key,event.handlers[ListenerID].callback)
2017-10-23 18:59:47 +08:00
end
function WaitEvent(EventName)
checkstring(EventName)
return event.pull(EventName)
end
function WaitEventFor(EventName,TimeOut)
checkstring(EventName)
checknumber(TimeOut)
return event.pull(TimeOut,EventName)
2017-10-23 19:09:15 +08:00
end
2017-10-23 19:42:40 +08:00
function PushEvent(EventName,...)
checkstring(EventName)
return event.push(EventName,...)
end
2017-10-23 19:09:15 +08:00
function AddTimer(Interval,CallbackFunction,Times)
checknumber(Interval)
checkfunction(CallbackFunction)
if(type(Times)~=nil) then -- Timer will run [Times] times.
checknumber(Times)
return event.timer(Interval,CallbackFunction,Times)
else -- Timer will run once.
return event.timer(Interval,CallbackFunction)
end
end
function RemoveTimer(TimerID)
checknumber(TimerID)
return event.cancel(TimerID)
end