Skip to content

Instantly share code, notes, and snippets.

@evgkul
Last active February 23, 2018 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evgkul/841c0828ff3c9e6084d9b66d71df2c88 to your computer and use it in GitHub Desktop.
Save evgkul/841c0828ff3c9e6084d9b66d71df2c88 to your computer and use it in GitHub Desktop.
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