Created
March 15, 2017 05:39
-
-
Save ericroy/187502713ce51076605eb0885646ec02 to your computer and use it in GitHub Desktop.
Simple saga-like pattern in Lua for Defold
This file contains 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
local M = {} | |
local traceback = debug.traceback | |
local yield = coroutine.yield | |
local resume = coroutine.resume | |
local create = coroutine.create | |
local status = coroutine.status | |
function M.create(f, ...) | |
local co = create(f) | |
local ok, err = resume(co, ...) | |
if not ok then | |
print(traceback(co, err)) | |
end | |
return status(co) ~= 'dead' and co or null | |
end | |
function M.continue(co, ...) | |
local ok, err = resume(co, ...) | |
if not ok then | |
print(traceback(co, err)) | |
end | |
return status(co) ~= 'dead' and co or null | |
end | |
function M.yield(...) | |
yield(...) | |
end | |
function M.take(message_id) | |
local id, message, sender | |
repeat | |
id, message, sender = yield() | |
until id == message_id | |
return id, message, sender | |
end | |
function M.take_any(...) | |
local msg_set = {} | |
for _, v in ipairs({...}) do | |
msg_set[v] = true | |
end | |
local id, message, sender | |
repeat | |
id, message, sender = yield() | |
until msg_set[id] | |
return id, message, sender | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment