Diff

util/async.lua @ 8681:0c077800cd70

util.async: Make parameters to async.runner() optional
author Matthew Wild <mwild1@gmail.com>
date Fri, 23 Mar 2018 14:02:33 +0000
parent 8669:2aa85b0cd2b8
child 8683:867ac771fb6e
line wrap: on
line diff
--- 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