├── LICENSE.md ├── example.lua └── sync.lua /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Yongkang Chen lx1988cyk#gmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /example.lua: -------------------------------------------------------------------------------- 1 | local sync = require "sync" 2 | 3 | local function async_test() 4 | print("async wait 1 sec, start time: " .. os.time()) 5 | LuaTimer.Add(1000, function() 6 | print("async wait 1 sec, done time: " .. os.time()) 7 | end) 8 | end 9 | 10 | local function sync_test() 11 | local sleep = sync(LuaTimer.Add) 12 | 13 | coroutine.wrap(function() 14 | print("sync wait 1 sec, start time: " .. os.time()) 15 | sleep(1000) 16 | print("sync wait 1 sec, done time: " .. os.time()) 17 | end)() 18 | end 19 | 20 | async_test() 21 | sync_test() 22 | -------------------------------------------------------------------------------- /sync.lua: -------------------------------------------------------------------------------- 1 | return function(func) 2 | return function(...) 3 | local co 4 | 5 | local arg = {...} 6 | local len = select("#", ...) + 1 7 | arg[len] = function(...) 8 | if co == nil then 9 | co = {...} 10 | else 11 | coroutine.resume(co, ...) 12 | end 13 | end 14 | 15 | func(unpack(arg, 1, len)) 16 | 17 | if co then 18 | return unpack(co) 19 | end 20 | 21 | co = coroutine.running() 22 | return coroutine.yield() 23 | end 24 | end --------------------------------------------------------------------------------