Commit 16d8cb44 authored by chrox's avatar chrox

add 'thread' return type from middleware call

which could be used to implement async http request like
[this](https://github.com/koreader/koreader/blob/master/plugins/kosync.koplugin/KOSyncClient.lua#L29).
parent a08ca076
......@@ -20,7 +20,12 @@ local function _enable_if (self, cond, mw, args)
local _m = require(mw)
assert(type(_m.call) == 'function', mw .. " without a function call")
local f = function (req)
return _m.call(args, req)
local res = _m.call(args, req)
if type(res) == 'thread' then
coroutine.yield()
res = select(2, coroutine.resume(res))
end
return res
end
local t = self.middlewares; t[#t+1] = { cond = cond, code = f }
end
......
......@@ -38,3 +38,23 @@ is( res, dummy_resp )
dummy_resp.status = 599
res = client:get_info()
is( res, dummy_resp )
local async_resp = { status = 201 }
package.loaded['Spore.Middleware.Async'] = {}
require('Spore.Middleware.Async').call = function(args, req)
local result
-- mock async task such as http request that runs this in callback
result = async_resp
coroutine.status(args.thread)
return coroutine.create(function() coroutine.yield(result) end)
end
local co = coroutine.create(function()
res = client:get_info()
is( res.status, async_resp.status )
end)
client:reset_middlewares()
client:enable('Async', {thread = co})
while coroutine.status(co) ~= 'dead' do
coroutine.resume(co)
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment