@inquirer/core
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -37,3 +37,3 @@ "use strict"; | ||
let sessionRl; | ||
const resetHookState = () => { | ||
function resetHookState() { | ||
hooks.length = 0; | ||
@@ -45,4 +45,4 @@ hooksCleanup.length = 0; | ||
sessionRl = undefined; | ||
}; | ||
const cleanupHook = (index) => { | ||
} | ||
function cleanupHook(index) { | ||
const cleanFn = hooksCleanup[index]; | ||
@@ -52,3 +52,19 @@ if (typeof cleanFn === 'function') { | ||
} | ||
}; | ||
} | ||
function mergeStateUpdates(fn) { | ||
const wrapped = (...args) => { | ||
let shouldUpdate = false; | ||
const oldHandleChange = handleChange; | ||
handleChange = () => { | ||
shouldUpdate = true; | ||
}; | ||
const returnValue = fn(...args); | ||
if (shouldUpdate) { | ||
oldHandleChange(); | ||
} | ||
handleChange = oldHandleChange; | ||
return returnValue; | ||
}; | ||
return wrapped; | ||
} | ||
function useState(defaultValue) { | ||
@@ -68,5 +84,8 @@ const _idx = index; | ||
(newValue) => { | ||
hooks[_idx] = newValue; | ||
// Trigger re-render | ||
handleChange(); | ||
// Noop if the value is still the same. | ||
if (hooks[_idx] !== newValue) { | ||
hooks[_idx] = newValue; | ||
// Trigger re-render | ||
handleChange(); | ||
} | ||
}, | ||
@@ -89,3 +108,3 @@ ]; | ||
if (hasChanged) { | ||
hooksEffect.push(() => { | ||
hooksEffect.push(mergeStateUpdates(() => { | ||
cleanupHook(_idx); | ||
@@ -97,3 +116,3 @@ const cleanFn = cb(rl); | ||
hooksCleanup[_idx] = cleanFn; | ||
}); | ||
})); | ||
} | ||
@@ -109,5 +128,5 @@ hooks[_idx] = depArray; | ||
useEffect(() => { | ||
const handler = (_input, event) => { | ||
const handler = mergeStateUpdates((_input, event) => { | ||
userHandler(event, rl); | ||
}; | ||
}); | ||
rl.input.on('keypress', handler); | ||
@@ -114,0 +133,0 @@ return () => { |
@@ -37,3 +37,3 @@ "use strict"; | ||
let sessionRl; | ||
const resetHookState = () => { | ||
function resetHookState() { | ||
hooks.length = 0; | ||
@@ -45,4 +45,4 @@ hooksCleanup.length = 0; | ||
sessionRl = undefined; | ||
}; | ||
const cleanupHook = (index) => { | ||
} | ||
function cleanupHook(index) { | ||
const cleanFn = hooksCleanup[index]; | ||
@@ -52,3 +52,19 @@ if (typeof cleanFn === 'function') { | ||
} | ||
}; | ||
} | ||
function mergeStateUpdates(fn) { | ||
const wrapped = (...args) => { | ||
let shouldUpdate = false; | ||
const oldHandleChange = handleChange; | ||
handleChange = () => { | ||
shouldUpdate = true; | ||
}; | ||
const returnValue = fn(...args); | ||
if (shouldUpdate) { | ||
oldHandleChange(); | ||
} | ||
handleChange = oldHandleChange; | ||
return returnValue; | ||
}; | ||
return wrapped; | ||
} | ||
function useState(defaultValue) { | ||
@@ -68,5 +84,8 @@ const _idx = index; | ||
(newValue) => { | ||
hooks[_idx] = newValue; | ||
// Trigger re-render | ||
handleChange(); | ||
// Noop if the value is still the same. | ||
if (hooks[_idx] !== newValue) { | ||
hooks[_idx] = newValue; | ||
// Trigger re-render | ||
handleChange(); | ||
} | ||
}, | ||
@@ -89,3 +108,3 @@ ]; | ||
if (hasChanged) { | ||
hooksEffect.push(() => { | ||
hooksEffect.push(mergeStateUpdates(() => { | ||
cleanupHook(_idx); | ||
@@ -97,3 +116,3 @@ const cleanFn = cb(rl); | ||
hooksCleanup[_idx] = cleanFn; | ||
}); | ||
})); | ||
} | ||
@@ -109,5 +128,5 @@ hooks[_idx] = depArray; | ||
useEffect(() => { | ||
const handler = (_input, event) => { | ||
const handler = mergeStateUpdates((_input, event) => { | ||
userHandler(event, rl); | ||
}; | ||
}); | ||
rl.input.on('keypress', handler); | ||
@@ -114,0 +133,0 @@ return () => { |
{ | ||
"name": "@inquirer/core", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"engines": { | ||
@@ -97,3 +97,3 @@ "node": ">=14.18.0" | ||
}, | ||
"gitHead": "d48ecde923e5875de631f7b50f67cfaf00384947" | ||
"gitHead": "ba98109480c442d261f243e4117243a9e2f12fd0" | ||
} |
Sorry, the diff of this file is not supported yet
57617
1140
71
5
8
31