mirror of
https://github.com/Kiritow/OpenComputerScripts.git
synced 2024-03-22 13:10:46 +08:00
Fix bugs in libevent
Thanks for the developers of OpenComputers~! Now libevent use os.sleep() to wait available events.
This commit is contained in:
parent
4fd6b54552
commit
bc635d5d69
44
libevent.lua
44
libevent.lua
|
@ -114,9 +114,6 @@ local function doEventTranslate(raw_event)
|
||||||
t["address"]=a
|
t["address"]=a
|
||||||
t["signalName"]=b
|
t["signalName"]=b
|
||||||
t["signalValue"]=c
|
t["signalValue"]=c
|
||||||
-- libevent events
|
|
||||||
elseif(name=="EventBus_NewEvent") then
|
|
||||||
t["id"]=a
|
|
||||||
-- libnetbox events
|
-- libnetbox events
|
||||||
elseif(name=="net_message") then
|
elseif(name=="net_message") then
|
||||||
t["receiverAddress"]=a
|
t["receiverAddress"]=a
|
||||||
|
@ -218,15 +215,6 @@ end
|
||||||
|
|
||||||
--- EventBus: Queued event bus.
|
--- EventBus: Queued event bus.
|
||||||
--- Notice that event bus can only handle event packages.
|
--- Notice that event bus can only handle event packages.
|
||||||
function CreateEventBus()
|
|
||||||
return
|
|
||||||
{
|
|
||||||
listeners={},
|
|
||||||
events={},
|
|
||||||
id=uuid.next()
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
function EventBusListen(t,event_name)
|
function EventBusListen(t,event_name)
|
||||||
checktable(t)
|
checktable(t)
|
||||||
checkstring(event_name)
|
checkstring(event_name)
|
||||||
|
@ -234,7 +222,6 @@ function EventBusListen(t,event_name)
|
||||||
AddEventListener(event_name,
|
AddEventListener(event_name,
|
||||||
function(epack)
|
function(epack)
|
||||||
table.insert(t.events,epack)
|
table.insert(t.events,epack)
|
||||||
event.push("EventBus_NewEvent",t.id)
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -252,27 +239,27 @@ function GetNextEvent(t,wait_second)
|
||||||
return e
|
return e
|
||||||
elseif(wait_second~=nil) then
|
elseif(wait_second~=nil) then
|
||||||
if(wait_second<0) then
|
if(wait_second<0) then
|
||||||
while true do
|
while t.events[1]==nil do
|
||||||
local ev=WaitEvent("EventBus_NewEvent")
|
os.sleep(1)
|
||||||
if(ev.id==t.id) then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local ev=WaitEvent("EventBus_NewEvent",wait_second)
|
local wait_second_left=wait_second
|
||||||
if(ev==nil) then
|
while t.events[1]==nil and wait_second_left>0 do
|
||||||
return nil
|
os.sleep(1)
|
||||||
elseif(ev.id~=t.id) then
|
wait_second_left=wait_second_left-1
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if(t.events[1]~=nil) then
|
||||||
local e=t.events[1]
|
local e=t.events[1]
|
||||||
table.remove(t.events,1)
|
table.remove(t.events,1)
|
||||||
return e
|
return e
|
||||||
else
|
else
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function DestroyEventBus(t)
|
function DestroyEventBus(t)
|
||||||
|
@ -281,3 +268,14 @@ function DestroyEventBus(t)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function CreateEventBus()
|
||||||
|
return
|
||||||
|
{
|
||||||
|
listeners={},
|
||||||
|
events={},
|
||||||
|
-- Enable using t:listen(...)
|
||||||
|
listen=EventBusListen,
|
||||||
|
next=GetNextEvent,
|
||||||
|
reset=DestroyEventBus
|
||||||
|
}
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user