diff options
Diffstat (limited to 'src/wasp-utils.el')
| -rw-r--r-- | src/wasp-utils.el | 32 |
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 |
