Diff

util/async.lua @ 8649:9246f64d6f1e

util.async: Add once() to create temporary runners
author Matthew Wild <mwild1@gmail.com>
date Thu, 22 Mar 2018 07:56:01 +0000
parent 8648:ca710a71d730
child 8651:1b7c5933b215
line wrap: on
line diff
--- a/util/async.lua	Thu Mar 22 07:46:23 2018 +0000
+++ b/util/async.lua	Thu Mar 22 07:56:01 2018 +0000
@@ -219,4 +219,24 @@
 	return pcall(checkthread);
 end
 
-return { ready = ready, waiter = waiter, guarder = guarder, runner = runner };
+local once; -- forward declaration
+do
+	local once_watchers = {
+		error = function (_, err)
+			error(err);
+		end;
+	};
+	local function once_runner(func) func(); end
+	function once(func)
+		local r = runner(func, once_watchers);
+		return r:run(func);
+	end
+end
+
+return {
+	once = once;
+	ready = ready;
+	waiter = waiter;
+	guarder = guarder;
+	runner = runner
+};