License: MIT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local resp = {} | |
resp._key = {} | |
local getcor = coroutine.running | |
local yield = coroutine.yield | |
local resume = coroutine.resume | |
local remove = table.remove | |
local status = coroutine.status | |
local unpack = unpack or table.unpack | |
resp.resume = function(...) | |
local s = {resume(...)} | |
if s[2]~=resp._key then | |
return unpack(s) | |
end | |
if s[3]==getcor() then | |
return true,unpack(s[5]) | |
end | |
local prev = s[4] | |
s[4]=getcor() | |
remove(s,1) | |
resume(prev,yield(unpack(s))) | |
end | |
resp.yieldto = function(cor,...) | |
if status(cor)~='normal' then error('Cannot yield to this coroutine!') end | |
return yield(resp._key,cor,getcor(),{...}) | |
end | |
coroutine.resume = resp.resume | |
coroutine.yieldto = resp.yieldto | |
return resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment