aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--spec/util_async_spec.lua10
-rw-r--r--util/async.lua22
2 files changed, 31 insertions, 1 deletions
diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua
index ac71e755..f7e469c9 100644
--- a/spec/util_async_spec.lua
+++ b/spec/util_async_spec.lua
@@ -593,4 +593,14 @@ describe("util.async", function()
assert.spy(r.watchers.error).was_not.called();
end);
end);
+
+ describe("#once()", function ()
+ it("should work", function ()
+ local f = spy.new(function ()
+ assert.truthy(async.ready());
+ end);
+ async.once(f);
+ assert.spy(f).was.called();
+ end);
+ end);
end);
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
+};