mafredri / zsh-async

Because your terminal should be able to perform tasks asynchronously without external tools!
MIT License
766 stars 34 forks source link

Is it possible to source scripts asynchronously? #65

Closed lindhe closed 1 year ago

lindhe commented 1 year ago

Hi!

I would like to source my autocompletion asynchronously. Is that possible with your framework? I tried to do so, but it doesn't seem to work. I guess functions run as subshells and that makes it hard to source things?

Cheers! :)

mafredri commented 1 year ago

Hi @lindhe! In theory, you can source it asynchronously, but since no state is shared between the worker (subshell) and the main shell, you would need to serialize and transfer that state between them which would be quite inefficient. 😄

There's still something that can be done, I'd say you have two options:

  1. Perform som "heavy work" in the worker and once it's done, source the script with input from the worker
  2. Use sched to delay the sourcing until after your shell is ready (see example)

An example of delaying execution/source until after the shell is ready:

doit() { export MYENV=1; sleep 5; }
sched +1 doit

Notice that I've added a sleep to demonstrate that this scheduled task will lock up your shell until it's done, but it's a solid option to defer some work for later.

lindhe commented 1 year ago

Thanks for the input! This sounds like quite an adventure, but perhaps I'll give it a shot some day I feel a little bit too cooky. :D