Skip to content

Commit

Permalink
Chore (core): Stylistic change
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Sep 9, 2023
1 parent 15a1085 commit 7ad8f19
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions packages/core/src/lib/create-prompt.mts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(
let cancel: () => void = () => {};
const answer = new CancelablePromise<Value>((resolve, reject) => {
withHooks(rl, (store) => {
const checkCursorPos = () => {
function checkCursorPos() {
screen.checkCursorPos();
};
}

const onExit = () => {
function onExit() {
try {
store.hooksCleanup.forEach((cleanFn) => {
cleanFn?.();
Expand All @@ -75,37 +75,36 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(

process.removeListener('SIGINT', onForceExit);
store.rl.input.removeListener('keypress', checkCursorPos);
};
}

cancel = () => {
onExit();

reject(new Error('Prompt was canceled'));
};

let shouldHandleExit = true;
const onForceExit = () => {
function onForceExit() {
if (shouldHandleExit) {
shouldHandleExit = false;
onExit();
reject(new Error('User force closed the prompt with CTRL+C'));
}
};
}

// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
process.on('SIGINT', onForceExit);

const done = (value: Value) => {
function done(value: Value) {
// Delay execution to let time to the hookCleanup functions to registers.
setImmediate(() => {
onExit();

// Finally we resolve our promise
resolve(value);
});
};
}

const workLoop = (resolvedConfig: Config & ResolvedPromptConfig) => {
function workLoop(resolvedConfig: Config & ResolvedPromptConfig) {
store.index = 0;
store.handleChange = () => workLoop(resolvedConfig);

Expand All @@ -121,7 +120,7 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(
onExit();
reject(err);
}
};
}

// TODO: we should display a loader while we get the default options.
getPromptConfig(config).then((resolvedConfig) => {
Expand Down

0 comments on commit 7ad8f19

Please sign in to comment.