Skip to content

Instantly share code, notes, and snippets.

@Srlion
Last active May 11, 2022 13:54
Show Gist options
  • Save Srlion/59919e1cf354a25ccd62a1a130b29b8c to your computer and use it in GitHub Desktop.
Save Srlion/59919e1cf354a25ccd62a1a130b29b8c to your computer and use it in GitHub Desktop.
--[[
https://github.com/yongkangchen/lua-await/blob/master/sync.lua
MIT License
Copyright (c) 2016 Yongkang Chen lx1988cyk#gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]--
--[[
Examples:
local async = include("async.lua")
local Sleep = function(t)
return async.Promise(function(rs)
timer.Simple(t, rs)
end)
end
local printAfterTime = async(function(time)
Sleep(time)
print("oOoOoO printed after " .. time .. " second(s)")
end)
printAfterTime(1)
local fetchGoogle = function() -- You don't have to use async here
return async.httpFetch("http://google.com")
end
async(function(...)
local status, body, len, headers, code = fetchGoogle()
if (not status) then print(body) return end
print(body, len, headers, code)
end)()
]]--
AddCSLuaFile()
local unpack = unpack
local select = select
local coroutine_resume = coroutine.resume
local coroutine_yield = coroutine.yield
local coroutine_running = coroutine.running
local coroutine_create = coroutine.create
local getCoroutine
do
local table_maxn = table.maxn
local cached = {}
local count = 0
function getCoroutine(fn, ...) -- some stuff from http://lua-users.org/lists/lua-l/2013-07/msg00752.html
local ok, err
if (count > 0) then
ok, err = coroutine_resume(cached[count], fn, ...)
cached[count] = nil
count = count - 1
if (not ok) then error(err, 0) end
else
local _cached
_cached = coroutine_create(function(...)
local arg = {...}
while true do
arg[1](unpack(arg, 2, table_maxn(arg)))
count = count + 1
cached[count] = _cached
arg = {coroutine_yield()}
end
end)
ok, err = coroutine_resume(_cached, fn, ...)
if (not ok) then error(err, 0) end
end
end
end
local async = {}
async.__index = async
setmetatable(async, {
__call = function(_, fn)
return function(...)
if (coroutine_running()) then
return fn(...)
end
getCoroutine(fn, ...)
end
end
})
function async.Promise(fn)
local argn = coroutine_running()
if (not argn) then
error("can't run this function in non async function")
end
local co, cb
cb = function(...)
if (not co) then
co, argn = {...}, select("#", ...)
else
local ok, err = coroutine_resume(argn, ...)
if (not ok) then error(err, 0) end
end
end
fn(cb)
if (co) then
return unpack(co, 1, argn)
end
co = true
return coroutine_yield()
end
do
local timer_Simple = timer.Simple
function async.Sleep(t)
return async.Promise(function(rs)
timer_Simple(t, rs)
end)
end
end
do
local http = http
function async.httpFetch(url, headers)
return async.Promise(function(rs)
http.Fetch(
url,
function(...)
rs(true, ...)
end,
function(...)
rs(false, ...)
end,
headers
)
end)
end
function async.httpPost(url, parameters, headers)
return async.Promise(function(rs)
http.Post(
url,
parameters,
function(...)
rs(true, ...)
end,
function(...)
rs(false, ...)
end,
headers
)
end)
end
end
return async
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment