diff --git a/packages/core/src/lib/create-prompt.mts b/packages/core/src/lib/create-prompt.mts index 68f13ee84..33b736809 100644 --- a/packages/core/src/lib/create-prompt.mts +++ b/packages/core/src/lib/create-prompt.mts @@ -31,7 +31,7 @@ export function createPrompt(view: ViewFunction) { let cancel: () => void = () => {}; const answer = new CancelablePromise((resolve, reject) => { - withHooks(rl, (store) => { + withHooks(rl, (cycle) => { function checkCursorPos() { screen.checkCursorPos(); } @@ -58,7 +58,7 @@ export function createPrompt(view: ViewFunction) { screen.done(); removeExitListener(); - store.rl.input.removeListener('keypress', checkCursorPos); + rl.input.removeListener('keypress', checkCursorPos); }); cancel = () => { @@ -76,9 +76,7 @@ export function createPrompt(view: ViewFunction) { }); } - function workLoop() { - store.index = 0; - + cycle(() => { try { const nextView = view(config, done); @@ -91,16 +89,13 @@ export function createPrompt(view: ViewFunction) { onExit(); reject(error); } - } - - store.handleChange = () => workLoop(); - workLoop(); + }); // Re-renders only happen when the state change; but the readline cursor could change position // and that also requires a re-render (and a manual one because we mute the streams). // We set the listener after the initial workLoop to avoid a double render if render triggered // by a state change sets the cursor to the right position. - store.rl.input.on('keypress', checkCursorPos); + rl.input.on('keypress', checkCursorPos); }); }); diff --git a/packages/core/src/lib/hook-engine.mts b/packages/core/src/lib/hook-engine.mts index d00259e1f..3d5837a95 100644 --- a/packages/core/src/lib/hook-engine.mts +++ b/packages/core/src/lib/hook-engine.mts @@ -28,10 +28,22 @@ function createStore(rl: InquirerReadline) { } // Run callback in with the hook engine setup. -export function withHooks(rl: InquirerReadline, cb: (store: HookStore) => void) { +export function withHooks( + rl: InquirerReadline, + cb: (cycle: (render: () => void) => void) => void, +) { const store = createStore(rl); return hookStorage.run(store, () => { - cb(store); + function cycle(render: () => void) { + store.handleChange = () => { + store.index = 0; + render(); + }; + + store.handleChange(); + } + + cb(cycle); }); }