Skip to content

Instantly share code, notes, and snippets.

@agladysh
Created April 18, 2011 20:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agladysh/926163 to your computer and use it in GitHub Desktop.
Save agladysh/926163 to your computer and use it in GitHub Desktop.
Example for using coroutine-based workarounds for blocking callbacks in Lua Alchemy (issue #121)
-- See http://code.google.com/p/lua-alchemy/issues/detail?id=121
-- Example. Actual implementation should reuse as much resources as possible.
-- Code is not tested.
local sleep = function(interval)
local timer = as3.class.flash.utils.Timer.new(interval)
local function callback()
if timer then
timer.stop()
timer.removeEventListener(
as3.class.flash.events.TimerEvent.TIMER, callback
)
timer = nil
coroutine.resume()
end
end
timer.addEventListener(as3.class.flash.events.TimerEvent.TIMER, callback)
timer.start()
as3.onclose(function(e)
if timer then
timer.stop()
timer.removeEventListener(
as3.class.flash.events.TimerEvent.TIMER, callback
)
timer = nil
end
end)
coro.yield_outer()
end
-- Usage:
do_something()
sleep(5000)
do_something_else()
-- Example. Actual implementation should reuse as much resources as possible.
-- Code is not tested.
local sleep = function(interval)
local done = false
local timer = as3.class.flash.utils.Timer.new(interval)
local function callback()
if timer then
timer.stop()
timer.removeEventListener(
as3.class.flash.events.TimerEvent.TIMER, callback
)
timer = nil
end
done = true
end
timer.addEventListener(as3.class.flash.events.TimerEvent.TIMER, callback)
timer.start()
as3.onclose(function(e)
if timer then
timer.stop()
timer.removeEventListener(
as3.class.flash.events.TimerEvent.TIMER, callback
)
timer = nil
end
end)
while not done do
as3.flyield()
end
end
-- Usage:
do_something()
sleep(5000)
do_something_else()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment