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


    local function param_pack( params, callback )
        table.insert(params, callback)
        return 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( ... )
            return coroutine.resume(co, ...)
        end
        local params = {...}
        local host, service = params[1], table.remove(params, 2)
        if type(params[#params]) == 'function' then
            params = table.remove(params)(params, callback)
        else
            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( ... )
        local func = select(-1, ...)
        assert(type(func)=='function', 'the last argument must be a function for coroutine process')
        local co = coroutine.create(func)
    
        local function process( ... )
            coroutine.resume(co, ...)
        end
        process(...)
        return process
    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)
    
    --]]
  • 相关阅读:
    【数据库功能测试】之shell脚本执行sql命令
    【数据库使用】 mysql服务启动脚本
    【数据库功能测试】之存储过程
    各类排序算法实现
    Poj1830开关问题,高斯消元
    Poj3370Halloween treats鸽巢原理
    Poj2356Find a multiple鸽巢原理
    Poj3145Harmony Forever线段树+鸽巢原理
    hiho16动态lca
    hiho15周离线lca
  • 原文地址:https://www.cnblogs.com/qianwen36/p/7071126.html
Copyright © 2020-2023  润新知