util/async.lua
changeset 11965 542a9a503073
parent 10935 558f0555ba02
child 11966 9a70a543c727
--- a/util/async.lua	Mon Nov 29 02:13:45 2021 +0100
+++ b/util/async.lua	Mon Nov 29 14:11:24 2021 +0000
@@ -11,6 +11,9 @@
 	return thread;
 end
 
+-- Configurable functions
+local schedule_task = nil; -- schedule_task(seconds, callback)
+
 local function runner_from_thread(thread)
 	local level = 0;
 	-- Find the 'level' of the top-most function (0 == current level, 1 == caller, ...)
@@ -118,6 +121,15 @@
 	end;
 end
 
+local function sleep(seconds)
+	if not schedule_task then
+		error("async.sleep() is not available - configure schedule function");
+	end
+	local wait, done = waiter();
+	schedule_task(seconds, done);
+	wait();
+end
+
 local runner_mt = {};
 runner_mt.__index = runner_mt;
 
@@ -272,4 +284,7 @@
 	runner = runner;
 	wait = wait_for; -- COMPAT w/trunk pre-0.12
 	wait_for = wait_for;
+	sleep = sleep;
+
+	set_schedule_function = function (new_schedule_function) schedule_task = new_schedule_function; end;
 };