yunque9/lua_probject/tolua_project/System/coroutine.lua

127 lines
2.4 KiB
Lua
Raw Normal View History

2025-05-24 14:29:14 +08:00
--------------------------------------------------------------------------------
-- Copyright (c) 2015 - 2016 , 蒙占志(topameng) topameng@gmail.com
-- All rights reserved.
-- Use, modification and distribution are subject to the "MIT License"
--------------------------------------------------------------------------------
local create = coroutine.create
local running = coroutine.running
local resume = coroutine.resume
local yield = coroutine.yield
local error = error
local unpack = unpack
local debug = debug
local FrameTimer = FrameTimer
local CoTimer = CoTimer
local comap = {}
2025-06-29 22:21:50 +08:00
setmetatable(comap, { __mode = "kv" })
2025-05-24 14:29:14 +08:00
2025-06-29 22:21:50 +08:00
function coroutine.start(f, ...)
2025-05-24 14:29:14 +08:00
local co = create(f)
2025-06-29 22:21:50 +08:00
2025-05-24 14:29:14 +08:00
if running() == nil then
local flag, msg = resume(co, ...)
2025-06-29 22:21:50 +08:00
if not flag then
2025-05-24 14:29:14 +08:00
error(debug.traceback(co, msg))
2025-06-29 22:21:50 +08:00
end
2025-05-24 14:29:14 +08:00
else
2025-06-29 22:21:50 +08:00
local args = { ... }
2025-05-24 14:29:14 +08:00
local timer = nil
2025-06-29 22:21:50 +08:00
local action = function()
local flag, msg = resume(co, unpack(args))
if not flag then
timer:Stop()
error(debug.traceback(co, msg))
end
2025-05-24 14:29:14 +08:00
end
2025-06-29 22:21:50 +08:00
2025-05-24 14:29:14 +08:00
timer = FrameTimer.New(action, 0, 1)
comap[co] = timer
2025-06-29 22:21:50 +08:00
timer:Start()
2025-05-24 14:29:14 +08:00
end
return co
end
function coroutine.wait(t, co, ...)
2025-06-29 22:21:50 +08:00
local args = { ... }
co = co or running()
2025-05-24 14:29:14 +08:00
local timer = nil
2025-06-29 22:21:50 +08:00
local action = function()
2025-05-24 14:29:14 +08:00
local flag, msg = resume(co, unpack(args))
2025-06-29 22:21:50 +08:00
if not flag then
timer:Stop()
error(debug.traceback(co, msg))
2025-05-24 14:29:14 +08:00
return
end
end
2025-06-29 22:21:50 +08:00
2025-05-24 14:29:14 +08:00
timer = CoTimer.New(action, t, 1)
2025-06-29 22:21:50 +08:00
comap[co] = timer
2025-05-24 14:29:14 +08:00
timer:Start()
return yield()
end
function coroutine.step(t, co, ...)
2025-06-29 22:21:50 +08:00
local args = { ... }
co = co or running()
2025-05-24 14:29:14 +08:00
local timer = nil
2025-06-29 22:21:50 +08:00
local action = function()
2025-05-24 14:29:14 +08:00
local flag, msg = resume(co, unpack(args))
2025-06-29 22:21:50 +08:00
if not flag then
timer:Stop()
2025-05-24 14:29:14 +08:00
error(debug.traceback(co, msg))
2025-06-29 22:21:50 +08:00
return
end
2025-05-24 14:29:14 +08:00
end
timer = FrameTimer.New(action, t or 1, 1)
comap[co] = timer
timer:Start()
return yield()
end
2025-06-29 22:21:50 +08:00
function coroutine.www(www, co)
co = co or running()
local timer = nil
local action = function()
if not www.isDone then
return
end
timer:Stop()
local flag, msg = resume(co)
if not flag then
error(debug.traceback(co, msg))
return
end
end
timer = FrameTimer.New(action, 1, -1)
comap[co] = timer
timer:Start()
return yield()
2025-05-24 14:29:14 +08:00
end
function coroutine.stop(co)
2025-06-29 22:21:50 +08:00
local timer = comap[co]
2025-05-24 14:29:14 +08:00
2025-06-29 22:21:50 +08:00
if timer ~= nil then
comap[co] = nil
timer:Stop()
end
2025-05-24 14:29:14 +08:00
end
function coroutine.stopAll()
2025-06-29 22:21:50 +08:00
for k, v in pairs(comap) do
2025-05-24 14:29:14 +08:00
coroutine.stop(k)
end
end