@inquirer/core 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -34,20 +34,36 @@ const hooksEffect = [];
34
34
  let index = 0;
35
35
  let handleChange = () => { };
36
36
  let sessionRl;
37
- const resetHookState = () => {
37
+ function resetHookState() {
38
38
  hooks.length = 0;
39
39
  hooksCleanup.length = 0;
40
40
  hooksEffect.length = 0;
41
41
  index = 0;
42
42
  handleChange = () => { };
43
43
  sessionRl = undefined;
44
- };
45
- const cleanupHook = (index) => {
44
+ }
45
+ function cleanupHook(index) {
46
46
  const cleanFn = hooksCleanup[index];
47
47
  if (typeof cleanFn === 'function') {
48
48
  cleanFn();
49
49
  }
50
- };
50
+ }
51
+ function mergeStateUpdates(fn) {
52
+ const wrapped = (...args) => {
53
+ let shouldUpdate = false;
54
+ const oldHandleChange = handleChange;
55
+ handleChange = () => {
56
+ shouldUpdate = true;
57
+ };
58
+ const returnValue = fn(...args);
59
+ if (shouldUpdate) {
60
+ oldHandleChange();
61
+ }
62
+ handleChange = oldHandleChange;
63
+ return returnValue;
64
+ };
65
+ return wrapped;
66
+ }
51
67
  function useState(defaultValue) {
52
68
  const _idx = index;
53
69
  index++;
@@ -62,9 +78,12 @@ function useState(defaultValue) {
62
78
  return [
63
79
  hooks[_idx],
64
80
  (newValue) => {
65
- hooks[_idx] = newValue;
66
- // Trigger re-render
67
- handleChange();
81
+ // Noop if the value is still the same.
82
+ if (hooks[_idx] !== newValue) {
83
+ hooks[_idx] = newValue;
84
+ // Trigger re-render
85
+ handleChange();
86
+ }
68
87
  },
69
88
  ];
70
89
  }
@@ -82,14 +101,14 @@ function useEffect(cb, depArray) {
82
101
  hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
83
102
  }
84
103
  if (hasChanged) {
85
- hooksEffect.push(() => {
104
+ hooksEffect.push(mergeStateUpdates(() => {
86
105
  cleanupHook(_idx);
87
106
  const cleanFn = cb(rl);
88
107
  if (cleanFn != null && typeof cleanFn !== 'function') {
89
108
  throw new Error('useEffect return value must be a cleanup function or nothing.');
90
109
  }
91
110
  hooksCleanup[_idx] = cleanFn;
92
- });
111
+ }));
93
112
  }
94
113
  hooks[_idx] = depArray;
95
114
  }
@@ -100,9 +119,9 @@ function useKeypress(userHandler) {
100
119
  throw new Error('useKeypress must be used within a prompt');
101
120
  }
102
121
  useEffect(() => {
103
- const handler = (_input, event) => {
122
+ const handler = mergeStateUpdates((_input, event) => {
104
123
  userHandler(event, rl);
105
- };
124
+ });
106
125
  rl.input.on('keypress', handler);
107
126
  return () => {
108
127
  rl.input.removeListener('keypress', handler);
@@ -13,20 +13,36 @@ const hooksEffect = [];
13
13
  let index = 0;
14
14
  let handleChange = () => { };
15
15
  let sessionRl;
16
- const resetHookState = () => {
16
+ function resetHookState() {
17
17
  hooks.length = 0;
18
18
  hooksCleanup.length = 0;
19
19
  hooksEffect.length = 0;
20
20
  index = 0;
21
21
  handleChange = () => { };
22
22
  sessionRl = undefined;
23
- };
24
- const cleanupHook = (index) => {
23
+ }
24
+ function cleanupHook(index) {
25
25
  const cleanFn = hooksCleanup[index];
26
26
  if (typeof cleanFn === 'function') {
27
27
  cleanFn();
28
28
  }
29
- };
29
+ }
30
+ function mergeStateUpdates(fn) {
31
+ const wrapped = (...args) => {
32
+ let shouldUpdate = false;
33
+ const oldHandleChange = handleChange;
34
+ handleChange = () => {
35
+ shouldUpdate = true;
36
+ };
37
+ const returnValue = fn(...args);
38
+ if (shouldUpdate) {
39
+ oldHandleChange();
40
+ }
41
+ handleChange = oldHandleChange;
42
+ return returnValue;
43
+ };
44
+ return wrapped;
45
+ }
30
46
  export function useState(defaultValue) {
31
47
  const _idx = index;
32
48
  index++;
@@ -41,9 +57,12 @@ export function useState(defaultValue) {
41
57
  return [
42
58
  hooks[_idx],
43
59
  (newValue) => {
44
- hooks[_idx] = newValue;
45
- // Trigger re-render
46
- handleChange();
60
+ // Noop if the value is still the same.
61
+ if (hooks[_idx] !== newValue) {
62
+ hooks[_idx] = newValue;
63
+ // Trigger re-render
64
+ handleChange();
65
+ }
47
66
  },
48
67
  ];
49
68
  }
@@ -60,14 +79,14 @@ export function useEffect(cb, depArray) {
60
79
  hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
61
80
  }
62
81
  if (hasChanged) {
63
- hooksEffect.push(() => {
82
+ hooksEffect.push(mergeStateUpdates(() => {
64
83
  cleanupHook(_idx);
65
84
  const cleanFn = cb(rl);
66
85
  if (cleanFn != null && typeof cleanFn !== 'function') {
67
86
  throw new Error('useEffect return value must be a cleanup function or nothing.');
68
87
  }
69
88
  hooksCleanup[_idx] = cleanFn;
70
- });
89
+ }));
71
90
  }
72
91
  hooks[_idx] = depArray;
73
92
  }
@@ -77,9 +96,9 @@ export function useKeypress(userHandler) {
77
96
  throw new Error('useKeypress must be used within a prompt');
78
97
  }
79
98
  useEffect(() => {
80
- const handler = (_input, event) => {
99
+ const handler = mergeStateUpdates((_input, event) => {
81
100
  userHandler(event, rl);
82
- };
101
+ });
83
102
  rl.input.on('keypress', handler);
84
103
  return () => {
85
104
  rl.input.removeListener('keypress', handler);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "engines": {
5
5
  "node": ">=14.18.0"
6
6
  },
@@ -94,5 +94,5 @@
94
94
  }
95
95
  }
96
96
  },
97
- "gitHead": "d48ecde923e5875de631f7b50f67cfaf00384947"
97
+ "gitHead": "ba98109480c442d261f243e4117243a9e2f12fd0"
98
98
  }