From c600e0f6f60245d4c5ed9287fb5f2ccbdf06d8f1 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 22 Mar 2018 07:56:01 +0000 Subject: util.async: Add once() to create temporary runners --- util/async.lua | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'util/async.lua') diff --git a/util/async.lua b/util/async.lua index 1930d1a9..8e75a524 100644 --- a/util/async.lua +++ b/util/async.lua @@ -219,4 +219,24 @@ local function ready() 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 +}; -- cgit v1.2.3