Skip to content

Commit

Permalink
Refactor(@inquirer/core) Wrap logic to clear all effects in the hooks…
Browse files Browse the repository at this point in the history
… engine
  • Loading branch information
SBoudrias committed Jul 29, 2024
1 parent 3871321 commit 6610cc9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/lib/create-prompt.mts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -42,11 +43,9 @@ export function createPrompt<Value, Config>(view: ViewFunction<Value, Config>) {
);
});

function onExit() {
const onExit = AsyncResource.bind(() => {
try {
store.hooksCleanup.forEach((cleanFn) => {
cleanFn?.();
});
effectScheduler.clearAll();
} catch (error) {
reject(error);
}
Expand All @@ -60,7 +59,7 @@ export function createPrompt<Value, Config>(view: ViewFunction<Value, Config>) {

removeExitListener();
store.rl.input.removeListener('keypress', checkCursorPos);
}
});

cancel = () => {
onExit();
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/lib/hook-engine.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};

0 comments on commit 6610cc9

Please sign in to comment.