Skip to content

Commit

Permalink
Added error handler to flow and added options table when creating flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Ritzl committed Sep 6, 2016
1 parent dd98914 commit a6f760c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
11 changes: 10 additions & 1 deletion examples/flow/flow.script
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function init(self)
msg.post("#", "abc")
flow.delay(0.2)
msg.post("#", "booo")
end, true)
end, { parallel = true })

print("9 This flow will continue to run without waiting for the parallel flow to finish")

Expand Down Expand Up @@ -86,6 +86,15 @@ function init(self)
flow.play_animation("#sprite", "green_walk_once")
print("17 DONE")
end)

flow(function()
print("In another flow")
flow.delay(1)
error("Crashing the flow")
print("This will not print")
end, nil, function(error)
print("Something went wrong in my flow!", error)
end)
end

function final(self)
Expand Down
4 changes: 2 additions & 2 deletions game.project
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[project]
title = Ludobits
version = 0.7.1
version = 0.7.2

[bootstrap]
main_collection = /examples/listener/listener.collectionc
main_collection = /examples/flow/flow.collectionc
render = /ludobits/r/split_screen.renderc

[input]
Expand Down
24 changes: 18 additions & 6 deletions ludobits/m/flow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,22 @@ end
-- within an existing flow the existing flow can either
-- wait for the new flow to finish or run in parallel
-- @param fn The function to run within the flow
-- @param parallel
-- @param options Key value pairs. Allowed keys:
-- parallel = true if running flow shouldn't wait for this flow
-- @param on_error Function to call if something goes wrong while
-- running the flow
-- @return The created flow instance
function M.start(fn, parallel)
function M.start(fn, options, on_error)
assert(fn, "You must provide a function")
local co = coroutine.running()
if not co or not instances[co] or parallel then
return create_or_get(coroutine.create(fn))
if not co or not instances[co] or (options and options.parallel) then
local created_instance = create_or_get(coroutine.create(fn))
created_instance.on_error = on_error
return created_instance
else
local running_instance = instances[co]
local created_instance = create_or_get(coroutine.create(fn))
created_instance.on_error = on_error
M.until_true(function()
return instances[created_instance.co] == nil
end)
Expand All @@ -115,7 +121,6 @@ function M.stop(instance)
for k,v in pairs(instances) do
if v.url == instance then
instances[k] = nil
break
end
end
end
Expand Down Expand Up @@ -351,7 +356,14 @@ function M.on_message(message_id, message, sender)
instance.state = RUNNING
local result = instance.result or {}
instance.result = nil
coroutine.resume(co, table_unpack(result))
local ok, error = coroutine.resume(co, table_unpack(result))
if not ok then
if instance.on_error then
instance.on_error(error)
else
print("Warning: Flow resulted in error", error)
end
end
return
end
end
Expand Down

0 comments on commit a6f760c

Please sign in to comment.