From 57b8f00759bedec39cd22b7deeb22e59de80fabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 11 Nov 2019 12:39:34 +0800 Subject: =?UTF-8?q?=E4=B8=8D=E5=8F=AFyield=E7=9A=84=E5=9C=B0=E6=96=B9?= =?UTF-8?q?=E8=AE=A9=E5=87=BA=E4=B8=8D=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-beta/src/await.lua | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/server-beta/src/await.lua b/server-beta/src/await.lua index 92db7f73..c744e1be 100644 --- a/server-beta/src/await.lua +++ b/server-beta/src/await.lua @@ -32,13 +32,13 @@ end --- 休眠一段时间 ---@param time number function m.sleep(time, ...) - local co, main = coroutine.running() - if main then + if not coroutine.isyieldable() then if m.errorHandle then - m.errorHandle(debug.traceback('Cant sleep in main thread')) + m.errorHandle(debug.traceback('Cannot yield')) end return end + local co = coroutine.running() timer.wait(time, function () return m.checkResult(co, coroutine.resume(co)) end) @@ -48,13 +48,10 @@ end --- 等待直到唤醒 ---@param callback function function m.wait(callback, ...) - local co, main = coroutine.running() - if main then - if m.errorHandle then - m.errorHandle(debug.traceback('Cant wait in main thread')) - end + if not coroutine.isyieldable() then return end + local co = coroutine.running() callback(function (...) return m.checkResult(co, coroutine.resume(co, ...)) end) @@ -63,13 +60,10 @@ end --- 延迟 function m.delay(getVersion) - local co, main = coroutine.running() - if main then - if m.errorHandle then - m.errorHandle(debug.traceback('Cant wait in main thread')) - end + if not coroutine.isyieldable() then return end + local co = coroutine.running() local version = getVersion and getVersion() m.delayQueue[#m.delayQueue+1] = function () if version == (getVersion and getVersion()) then -- cgit v1.2.3