Skip to content

Commit

Permalink
Chore (Core): Cleanup some code to reduce moving parts
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Jun 3, 2023
1 parent 09539fa commit 5eaf72e
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/core/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ let index = 0;
let handleChange = () => {};
let sessionRl: InquirerReadline | void;

const resetHookState = () => {
hooks.length = 0;
hooksCleanup.length = 0;
hooksEffect.length = 0;
index = 0;
handleChange = () => {};
sessionRl = undefined;
};

const cleanupHook = (index: number) => {
const cleanFn = hooksCleanup[index];
if (typeof cleanFn === 'function') {
Expand Down Expand Up @@ -125,23 +134,22 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(
) => string | [string, string | undefined]
) {
const prompt: Prompt<Value, Config> = async (config, context) => {
// Set our state before starting the prompt.
resetHookState();

// Default `input` to stdin
const input = context?.input ?? process.stdin;

// Add mute capabilities to the output
const output = new MuteStream();
output.pipe(context?.output ?? process.stdout);

const rl = readline.createInterface({
sessionRl = readline.createInterface({
terminal: true,
input,
output,
}) as InquirerReadline;
const screen = new ScreenManager(rl);

// Set our state before starting the prompt.
hooks.length = 0;
sessionRl = rl;
const screen = new ScreenManager(sessionRl);

// TODO: we should display a loader while we get the default options.

Check warning on line 154 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'

Check warning on line 154 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'

Check warning on line 154 in packages/core/src/index.mts

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected 'todo' comment: 'TODO: we should display a loader while...'
const resolvedConfig = await getPromptConfig(config);
Expand Down Expand Up @@ -184,13 +192,6 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>(

onExit();

// Reset hooks state
index = 0;
hooks.length = 0;
hooksEffect.length = 0;
hooksCleanup.length = 0;
sessionRl = undefined;

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

0 comments on commit 5eaf72e

Please sign in to comment.