Skip to content

Instantly share code, notes, and snippets.

@evgkul
Last active Feb 23, 2018
Embed
What would you like to do?
License: MIT
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