@inquirer/core 2.0.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 +35 -12
- package/dist/cjs/types/index.d.cts +1 -1
- package/dist/esm/index.mjs +35 -12
- package/dist/esm/types/index.d.mts +1 -1
- package/package.json +2 -2
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
|
-
|
|
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
|
-
|
|
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,14 +78,21 @@ function useState(defaultValue) {
|
|
|
62
78
|
return [
|
|
63
79
|
hooks[_idx],
|
|
64
80
|
(newValue) => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
}
|
|
71
90
|
exports.useState = useState;
|
|
72
91
|
function useEffect(cb, depArray) {
|
|
92
|
+
const rl = sessionRl;
|
|
93
|
+
if (!rl) {
|
|
94
|
+
throw new Error('useEffect must be used within a prompt');
|
|
95
|
+
}
|
|
73
96
|
const _idx = index;
|
|
74
97
|
index++;
|
|
75
98
|
const oldDeps = hooks[_idx];
|
|
@@ -78,14 +101,14 @@ function useEffect(cb, depArray) {
|
|
|
78
101
|
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
79
102
|
}
|
|
80
103
|
if (hasChanged) {
|
|
81
|
-
hooksEffect.push(() => {
|
|
104
|
+
hooksEffect.push(mergeStateUpdates(() => {
|
|
82
105
|
cleanupHook(_idx);
|
|
83
|
-
const cleanFn = cb();
|
|
106
|
+
const cleanFn = cb(rl);
|
|
84
107
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
85
108
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
86
109
|
}
|
|
87
110
|
hooksCleanup[_idx] = cleanFn;
|
|
88
|
-
});
|
|
111
|
+
}));
|
|
89
112
|
}
|
|
90
113
|
hooks[_idx] = depArray;
|
|
91
114
|
}
|
|
@@ -96,9 +119,9 @@ function useKeypress(userHandler) {
|
|
|
96
119
|
throw new Error('useKeypress must be used within a prompt');
|
|
97
120
|
}
|
|
98
121
|
useEffect(() => {
|
|
99
|
-
const handler = (_input, event) => {
|
|
122
|
+
const handler = mergeStateUpdates((_input, event) => {
|
|
100
123
|
userHandler(event, rl);
|
|
101
|
-
};
|
|
124
|
+
});
|
|
102
125
|
rl.input.on('keypress', handler);
|
|
103
126
|
return () => {
|
|
104
127
|
rl.input.removeListener('keypress', handler);
|
|
@@ -18,7 +18,7 @@ export type KeypressEvent = {
|
|
|
18
18
|
};
|
|
19
19
|
type NotFunction<T> = T extends Function ? never : T;
|
|
20
20
|
export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
|
|
21
|
-
export declare function useEffect(cb: () => void | (() => void), depArray: unknown[]): void;
|
|
21
|
+
export declare function useEffect(cb: (rl: InquirerReadline) => void | (() => void), depArray: unknown[]): void;
|
|
22
22
|
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
23
23
|
export declare function useRef<Value>(val: Value): {
|
|
24
24
|
current: Value;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -13,20 +13,36 @@ const hooksEffect = [];
|
|
|
13
13
|
let index = 0;
|
|
14
14
|
let handleChange = () => { };
|
|
15
15
|
let sessionRl;
|
|
16
|
-
|
|
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
|
-
|
|
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,13 +57,20 @@ export function useState(defaultValue) {
|
|
|
41
57
|
return [
|
|
42
58
|
hooks[_idx],
|
|
43
59
|
(newValue) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
}
|
|
50
69
|
export function useEffect(cb, depArray) {
|
|
70
|
+
const rl = sessionRl;
|
|
71
|
+
if (!rl) {
|
|
72
|
+
throw new Error('useEffect must be used within a prompt');
|
|
73
|
+
}
|
|
51
74
|
const _idx = index;
|
|
52
75
|
index++;
|
|
53
76
|
const oldDeps = hooks[_idx];
|
|
@@ -56,14 +79,14 @@ export function useEffect(cb, depArray) {
|
|
|
56
79
|
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
57
80
|
}
|
|
58
81
|
if (hasChanged) {
|
|
59
|
-
hooksEffect.push(() => {
|
|
82
|
+
hooksEffect.push(mergeStateUpdates(() => {
|
|
60
83
|
cleanupHook(_idx);
|
|
61
|
-
const cleanFn = cb();
|
|
84
|
+
const cleanFn = cb(rl);
|
|
62
85
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
63
86
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
64
87
|
}
|
|
65
88
|
hooksCleanup[_idx] = cleanFn;
|
|
66
|
-
});
|
|
89
|
+
}));
|
|
67
90
|
}
|
|
68
91
|
hooks[_idx] = depArray;
|
|
69
92
|
}
|
|
@@ -73,9 +96,9 @@ export function useKeypress(userHandler) {
|
|
|
73
96
|
throw new Error('useKeypress must be used within a prompt');
|
|
74
97
|
}
|
|
75
98
|
useEffect(() => {
|
|
76
|
-
const handler = (_input, event) => {
|
|
99
|
+
const handler = mergeStateUpdates((_input, event) => {
|
|
77
100
|
userHandler(event, rl);
|
|
78
|
-
};
|
|
101
|
+
});
|
|
79
102
|
rl.input.on('keypress', handler);
|
|
80
103
|
return () => {
|
|
81
104
|
rl.input.removeListener('keypress', handler);
|
|
@@ -18,7 +18,7 @@ export type KeypressEvent = {
|
|
|
18
18
|
};
|
|
19
19
|
type NotFunction<T> = T extends Function ? never : T;
|
|
20
20
|
export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
|
|
21
|
-
export declare function useEffect(cb: () => void | (() => void), depArray: unknown[]): void;
|
|
21
|
+
export declare function useEffect(cb: (rl: InquirerReadline) => void | (() => void), depArray: unknown[]): void;
|
|
22
22
|
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
23
23
|
export declare function useRef<Value>(val: Value): {
|
|
24
24
|
current: Value;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/core",
|
|
3
|
-
"version": "2.
|
|
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": "
|
|
97
|
+
"gitHead": "ba98109480c442d261f243e4117243a9e2f12fd0"
|
|
98
98
|
}
|