From b3b89b36058f992d2b0046cc62fc5344309088c3 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 10 Mar 2018 00:18:16 +0800 Subject: [PATCH] Add wait_ratio option to GetNextEvent() This should make event listener queue work faster. Check per second (cps) is limited up to 20. --- libevent.lua | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/libevent.lua b/libevent.lua index 276f218..9893017 100644 --- a/libevent.lua +++ b/libevent.lua @@ -332,7 +332,7 @@ function EventBusListen(t,event_name) ) end -function GetNextEvent(t,wait_second) +function GetNextEvent(t,wait_second,wait_ratio) checktable(t) if(wait_second~=nil) then checknumber(wait_second) @@ -342,6 +342,20 @@ function GetNextEvent(t,wait_second) wait_second=-1 end + if(wait_ratio~=nil) then + checknumber(wait_ratio) + + if(wait_ratio>1) then + wait_ratio=1/wait_ratio + end + + if(wait_ratio<0.05) then + error("Wait ratio should greater than 0.05. (cps less than 20)") + end + else + wait_ratio=1 + end + if(t.events[1]~=nil) then local e=t.events[1] table.remove(t.events,1) @@ -349,13 +363,13 @@ function GetNextEvent(t,wait_second) else if(wait_second<0) then while t.events[1]==nil do - os.sleep(1) + os.sleep(wait_ratio) end else local wait_second_left=wait_second while t.events[1]==nil and wait_second_left>0 do - os.sleep(1) - wait_second_left=wait_second_left-1 + os.sleep(wait_ratio) + wait_second_left=wait_second_left-wait_ratio end end