jwiegley / emacs-async

Simple library for asynchronous processing in Emacs
GNU General Public License v3.0
828 stars 68 forks source link

async-inject-function? #122

Closed jethrokuan closed 4 years ago

jethrokuan commented 4 years ago

I have a few functions defined outside of my async-start call, that's used both synchronously and asynchronously. I'd like these functions to be available within the async-start lambda, would it be possible to add a async-inject-function similar to what async-inject-variable does?

see https://github.com/jethrokuan/org-roam/pull/72

thierryvolpiatto commented 4 years ago

Jethro Kuan notifications@github.com writes:

I have a few functions defined outside of my async-start call, that's used both synchronously and asynchronously. I'd like these functions to be available within the async-start lambda, would it be possible to add a async-inject-function similar to what async-inject-variable does?

The simplest way is to put your functions inside a file and require or load file in your async-start lambda, another way is to define your function as a defalias always in your async-start lambda:

Example:

(defun tv/shuffle-vector (vector)
  "Shuffle VECTOR."
  (cl-loop with len = (1- (length vector))
           while (>= len 0)
           for rand = (random (1+ len))
           for old = (aref vector rand)
           do (progn
                (aset vector rand (aref vector len))
                (aset vector len old)
                (setq len (1- len)))
           finally return vector))

(async-start `(lambda ()
               (defalias 'tv/shuffle-vector
                 ,(symbol-function 'tv/shuffle-vector))
               (tv/shuffle-vector ["a" "b" "c" "d" "e" "f" "g" "h"]))
             (lambda (result)
               (message "Result: %S" result)))

-- Thierry

Get my Gnupg key: gpg --keyserver pgp.mit.edu --recv-keys 59F29997

jethrokuan commented 4 years ago

Thanks a lot @thierryvolpiatto , I'll adopt the latter!