码迷,mamicode.com
首页 > 其他好文 > 详细

[lua]异步串行流程*协程

时间:2017-06-23 19:23:20      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:...   service   running   proc   call   out   func   tostring   blog   

local function param_pack( params, callback )
    local host = params[1]
    local service = table.remove(params, 2)
    table.insert(params, callback)
    return host, service, params
end

local function asyncall( ... )
    local co, main = coroutine.running()
    if main then
        print(Please use .call(...) in .run(func) context)
        return
    end
    local function callback( ... )
        local params = {co, ... }
        return coroutine.resume(unpack(params))
    end
    local host, service
    local params = {...}
    if type(params[#params]) == function then
        host, service, params = table.remove(params)(params, callback)
    else
        host, service, params = param_pack(params, callback)
    end
    if type(host[service]) == function then
        return coroutine.yield(host[service](unpack(params)))
    else
        print(service:..service.. not implement at ..tostring(host))
    end
end

local function runProcess( func, ... )
    local co = coroutine.create(func)

    local params = {co, ...}
    return coroutine.resume(unpack(params))
end

local target = {
    call = asyncall,
    run = runProcess
}

return target

--[[
-- example
local Plugin = plugin.AgentManager:getUserPlugin()
target.run(function ( ... )
    local code, msg, info = target.call(Plugin, ‘queryThirdInfo‘, ‘weixin‘)
    if code == AsyncQueryStatus.kSuccess then
        dump(info)
    else
        print(msg)
    end
    code, msg = target.call(Plugin, ‘queryThirdAccountBindState‘, ‘weixin‘)
end)

--]]

 

[lua]异步串行流程*协程

标签:...   service   running   proc   call   out   func   tostring   blog   

原文地址:http://www.cnblogs.com/qianwen36/p/7071126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!