# HG changeset patch # User Matthew Wild # Date 1521813753 0 # Node ID 0c077800cd708e0646a0c8e98d94c7805f2c4197 # Parent 1cc9a293e958560e9273b237a8cd0ed22ceaff91 util.async: Make parameters to async.runner() optional diff -r 1cc9a293e958 -r 0c077800cd70 spec/util_async_spec.lua --- a/spec/util_async_spec.lua Fri Mar 23 14:01:42 2018 +0100 +++ b/spec/util_async_spec.lua Fri Mar 23 14:02:33 2018 +0000 @@ -94,6 +94,26 @@ assert.equal(last_item, values[#values]); end); + it("should work with no parameters", function () + local item = "fail"; + local r = async.runner(); + local f = spy.new(function () item = "success"; end); + r:run(f); + assert.spy(f).was.called(); + assert.equal(item, "success"); + end); + + it("supports a default error handler", function () + local item = "fail"; + local r = async.runner(); + local f = spy.new(function () error("test error"); end); + assert.error_matches(function () + r:run(f); + end, "test error"); + assert.spy(f).was.called(); + assert.equal(item, "fail"); + end); + describe("#errors", function () describe("should notify", function () local last_processed_item, last_error; diff -r 1cc9a293e958 -r 0c077800cd70 util/async.lua --- a/util/async.lua Fri Mar 23 14:01:42 2018 +0100 +++ b/util/async.lua Fri Mar 23 14:02:33 2018 +0000 @@ -130,10 +130,14 @@ return thread; end -local empty_watchers = {}; +local function default_error_watcher(runner, err) + runner:log("error", "Encountered error: %s", err); + error(err); +end +local function default_func(f) f(); end local function runner(func, watchers, data) - return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready", - queue = {}, watchers = watchers or empty_watchers, data = data, id = new_id() } + return setmetatable({ func = func or default_func, thread = false, state = "ready", notified_state = "ready", + queue = {}, watchers = watchers or { error = default_error_watcher }, data = data, id = new_id() } , runner_mt); end