Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(async): avoid blocking the shell while waiting #12304

Merged
merged 3 commits into from Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 5 additions & 9 deletions lib/async_prompt.zsh
Expand Up @@ -82,10 +82,8 @@ function _omz_async_request {
exec {fd}< <(
# Tell parent process our PID
builtin echo ${sysparams[pid]}
# Store handler name for callback
builtin echo $handler
# Set exit code for the handler if used
(exit $ret)
() { return $ret }
# Run the async function handler
$handler
)
Expand All @@ -98,8 +96,7 @@ function _omz_async_request {
command true

# Save the PID from the handler child process
read pid <&$fd
_OMZ_ASYNC_PIDS[$handler]=$pid
read -u $fd "_OMZ_ASYNC_PIDS[$handler]"

# When the fd is readable, call the response handler
zle -F "$fd" _omz_async_callback
Expand All @@ -114,15 +111,14 @@ function _omz_async_callback() {
local err=$2 # Second arg will be passed in case of error

if [[ -z "$err" || "$err" == "hup" ]]; then
# Get handler name from first line
local handler
read handler <&$fd
# Get handler name from fd
local handler="${(k)_OMZ_ASYNC_FDS[(r)$fd]}"

# Store old output which is supposed to be already printed
local old_output="${_OMZ_ASYNC_OUTPUT[$handler]}"

# Read output from fd
_OMZ_ASYNC_OUTPUT[$handler]="$(cat <&$fd)"
IFS= read -r -u $fd -d '' "_OMZ_ASYNC_OUTPUT[$handler]"

# Repaint prompt if output has changed
if [[ "$old_output" != "${_OMZ_ASYNC_OUTPUT[$handler]}" ]]; then
Expand Down