summaryrefslogtreecommitdiff
path: root/src/wasp-utils.el
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2026-09-22 00:06:29 -0400
committerLLLL Colonq <llll@colonq>2026-09-22 00:06:29 -0400
commit7abb751eb3d9213ba690ed4ae6b0dbbc099d8e46 (patch)
treec051022c2eca4373233a874d1d714d58e807d97e /src/wasp-utils.el
parent07f5a19d7930d1d912d05c95d9cc5b4b0c407383 (diff)
Diffstat (limited to 'src/wasp-utils.el')
-rw-r--r--src/wasp-utils.el32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/wasp-utils.el b/src/wasp-utils.el
index 9f29f6dc..f588d5f4 100644
--- a/src/wasp-utils.el
+++ b/src/wasp-utils.el
@@ -393,5 +393,37 @@ Otherwise make it invisible."
"Generate a UUID."
(s-trim (shell-command-to-string "uuidgen")))
+(defun w/async-all-helper (hs cb)
+ "Take N functions HS and a single callback with N arguments CB.
+Run all N functions and when they all finish pass all results to the callback.
+The callback is the last element of HANDLERS."
+ (let*
+ ( (len (length hs))
+ (finished 0)
+ (results (make-vector len nil)))
+ (--each-indexed hs
+ (funcall it
+ (lambda (res)
+ (setf (aref results it-index) res)
+ (incf finished)
+ (cond
+ ((> finished len) (error "Received more responses than expected in w/async-all!"))
+ ((= finished len)
+ (apply cb (seq-into results 'list)))))))))
+
+(defmacro w/async-all (&rest forms)
+ "Run the first FORMS in parallel and pass the results to the last form."
+ (let*
+ ( (cb (-last-item forms))
+ (len (- (length forms) 1))
+ (hs (-take len forms)))
+ `(w/async-all-helper
+ (list
+ ,@(--map
+ (let ((arg (gensym "w/async-all-arg")))
+ `(lambda (,arg) ,(-concat it (list arg))))
+ hs))
+ ,cb)))
+
(provide 'wasp-utils)
;;; wasp-utils.el ends here