diff --git a/packages/core/src/lib/create-prompt.mts b/packages/core/src/lib/create-prompt.mts index f7eb47da0..68f13ee84 100644 --- a/packages/core/src/lib/create-prompt.mts +++ b/packages/core/src/lib/create-prompt.mts @@ -1,4 +1,5 @@ import * as readline from 'node:readline'; +import { AsyncResource } from 'node:async_hooks'; import { CancelablePromise, type Prompt, type Prettify } from '@inquirer/type'; import MuteStream from 'mute-stream'; import { onExit as onSignalExit } from 'signal-exit'; @@ -42,11 +43,9 @@ export function createPrompt(view: ViewFunction) { ); }); - function onExit() { + const onExit = AsyncResource.bind(() => { try { - store.hooksCleanup.forEach((cleanFn) => { - cleanFn?.(); - }); + effectScheduler.clearAll(); } catch (error) { reject(error); } @@ -60,7 +59,7 @@ export function createPrompt(view: ViewFunction) { removeExitListener(); store.rl.input.removeListener('keypress', checkCursorPos); - } + }); cancel = () => { onExit(); diff --git a/packages/core/src/lib/hook-engine.mts b/packages/core/src/lib/hook-engine.mts index d99fd6aa1..d00259e1f 100644 --- a/packages/core/src/lib/hook-engine.mts +++ b/packages/core/src/lib/hook-engine.mts @@ -140,4 +140,13 @@ export const effectScheduler = { store.hooksEffect.length = 0; })(); }, + clearAll() { + const store = getStore(); + store.hooksCleanup.forEach((cleanFn) => { + cleanFn?.(); + }); + + store.hooksEffect.length = 0; + store.hooksCleanup.length = 0; + }, };