Created
April 18, 2011 20:59
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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