Skip to content

Commit

Permalink
Only set ZLE watcher after a callback has been registered
Browse files Browse the repository at this point in the history
Fixes #38.
  • Loading branch information
mafredri committed Apr 13, 2020
1 parent 361dc17 commit 317d19e
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions async.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ _async_send_job() {
shift 2

zpty -t $worker &>/dev/null || {
typeset -gA ASYNC_PTYS ASYNC_CALLBACKS
typeset -gA ASYNC_CALLBACKS
local callback=$ASYNC_CALLBACKS[$worker]

if [[ -n $callback ]]; then
Expand Down Expand Up @@ -453,7 +453,7 @@ _async_notify_trap() {
async_register_callback() {
setopt localoptions noshwordsplit nolocaltraps

typeset -gA ASYNC_CALLBACKS
typeset -gA ASYNC_PTYS ASYNC_CALLBACKS
local worker=$1; shift

ASYNC_CALLBACKS[$worker]="$*"
Expand All @@ -462,6 +462,14 @@ async_register_callback() {
# workers to notify (via -n) when a job is done.
if [[ ! -o interactive ]] || [[ ! -o zle ]]; then
trap '_async_notify_trap' WINCH
elif [[ -o interactive ]] && [[ -o zle ]]; then
local fd w
for fd w in ${(@kv)ASYNC_PTYS}; do
if [[ $w == $worker ]]; then
zle -F $fd _async_zle_watcher # Register the ZLE handler.
break
fi
done
fi
}

Expand Down Expand Up @@ -575,8 +583,7 @@ async_start_worker() {
REPLY=$zptyfd # Use the guessed value for the file desciptor.
fi

ASYNC_PTYS[$REPLY]=$worker # Map the file desciptor to the worker.
zle -F $REPLY _async_zle_watcher # Register the ZLE handler.
ASYNC_PTYS[$REPLY]=$worker # Map the file desciptor to the worker.
fi
}

Expand Down

0 comments on commit 317d19e

Please sign in to comment.