From 05c67040757bd1c8861caa03755609a42b504ecf Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 1 Apr 2023 11:59:50 +0100 Subject: moduleapi: Add module:once() to execute a function after module load/startup It is a common pattern for modules to do something like check for prosody.start_time, and execute code immediately if it is present, or wait for the server-started event if it isn't yet. For example, this allows you to run code after all other modules/hosts have been loaded, that are going to be loaded. Such code can now be replaced with a simple call to this method. --- core/moduleapi.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'core') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 00b2ae16..8479284d 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -693,4 +693,9 @@ function api:may(action, context, peek) end end +-- Execute a function, once, but only after startup is complete +function api:once(f) + return prosody.started:next(f); +end + return api; -- cgit v1.2.3