@inquirer/core 9.0.6 → 9.0.8

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.
@@ -28,6 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.createPrompt = createPrompt;
30
30
  const readline = __importStar(require("node:readline"));
31
+ const node_async_hooks_1 = require("node:async_hooks");
31
32
  const type_1 = require("@inquirer/type");
32
33
  const mute_stream_1 = __importDefault(require("mute-stream"));
33
34
  const signal_exit_1 = require("signal-exit");
@@ -50,7 +51,7 @@ function createPrompt(view) {
50
51
  const screen = new screen_manager_mjs_1.default(rl);
51
52
  let cancel = () => { };
52
53
  const answer = new type_1.CancelablePromise((resolve, reject) => {
53
- (0, hook_engine_mjs_1.withHooks)(rl, (store) => {
54
+ (0, hook_engine_mjs_1.withHooks)(rl, (cycle) => {
54
55
  function checkCursorPos() {
55
56
  screen.checkCursorPos();
56
57
  }
@@ -58,15 +59,16 @@ function createPrompt(view) {
58
59
  onExit();
59
60
  reject(new errors_mjs_1.ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
60
61
  });
61
- function onExit() {
62
+ const hooksCleanup = node_async_hooks_1.AsyncResource.bind(() => {
62
63
  try {
63
- store.hooksCleanup.forEach((cleanFn) => {
64
- cleanFn === null || cleanFn === void 0 ? void 0 : cleanFn();
65
- });
64
+ hook_engine_mjs_1.effectScheduler.clearAll();
66
65
  }
67
66
  catch (error) {
68
67
  reject(error);
69
68
  }
69
+ });
70
+ function onExit() {
71
+ hooksCleanup();
70
72
  if (context === null || context === void 0 ? void 0 : context.clearPromptOnDone) {
71
73
  screen.clean();
72
74
  }
@@ -75,7 +77,8 @@ function createPrompt(view) {
75
77
  }
76
78
  screen.done();
77
79
  removeExitListener();
78
- store.rl.input.removeListener('keypress', checkCursorPos);
80
+ rl.input.removeListener('keypress', checkCursorPos);
81
+ rl.removeListener('close', hooksCleanup);
79
82
  }
80
83
  cancel = () => {
81
84
  onExit();
@@ -89,8 +92,7 @@ function createPrompt(view) {
89
92
  resolve(value);
90
93
  });
91
94
  }
92
- function workLoop() {
93
- store.index = 0;
95
+ cycle(() => {
94
96
  try {
95
97
  const nextView = view(config, done);
96
98
  const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
@@ -101,14 +103,16 @@ function createPrompt(view) {
101
103
  onExit();
102
104
  reject(error);
103
105
  }
104
- }
105
- store.handleChange = () => workLoop();
106
- workLoop();
106
+ });
107
107
  // Re-renders only happen when the state change; but the readline cursor could change position
108
108
  // and that also requires a re-render (and a manual one because we mute the streams).
109
109
  // We set the listener after the initial workLoop to avoid a double render if render triggered
110
110
  // by a state change sets the cursor to the right position.
111
- store.rl.input.on('keypress', checkCursorPos);
111
+ rl.input.on('keypress', checkCursorPos);
112
+ // The close event triggers immediately when the user press ctrl+c. SignalExit on the other hand
113
+ // triggers after the process is done (which happens after timeouts are done triggering.)
114
+ // We triggers the hooks cleanup phase on rl `close` so active timeouts can be cleared.
115
+ rl.on('close', hooksCleanup);
112
116
  });
113
117
  });
114
118
  answer.cancel = cancel;
@@ -25,7 +25,14 @@ function createStore(rl) {
25
25
  function withHooks(rl, cb) {
26
26
  const store = createStore(rl);
27
27
  return hookStorage.run(store, () => {
28
- cb(store);
28
+ function cycle(render) {
29
+ store.handleChange = () => {
30
+ store.index = 0;
31
+ render();
32
+ };
33
+ store.handleChange();
34
+ }
35
+ cb(cycle);
29
36
  });
30
37
  }
31
38
  // Safe getStore utility that'll return the store or throw if undefined.
@@ -101,4 +108,12 @@ exports.effectScheduler = {
101
108
  store.hooksEffect.length = 0;
102
109
  })();
103
110
  },
111
+ clearAll() {
112
+ const store = getStore();
113
+ store.hooksCleanup.forEach((cleanFn) => {
114
+ cleanFn === null || cleanFn === void 0 ? void 0 : cleanFn();
115
+ });
116
+ store.hooksEffect.length = 0;
117
+ store.hooksCleanup.length = 0;
118
+ },
104
119
  };
@@ -8,11 +8,15 @@ function useKeypress(userHandler) {
8
8
  const signal = (0, use_ref_mjs_1.useRef)(userHandler);
9
9
  signal.current = userHandler;
10
10
  (0, use_effect_mjs_1.useEffect)((rl) => {
11
+ let ignore = false;
11
12
  const handler = (0, hook_engine_mjs_1.withUpdates)((_input, event) => {
13
+ if (ignore)
14
+ return;
12
15
  signal.current(event, rl);
13
16
  });
14
17
  rl.input.on('keypress', handler);
15
18
  return () => {
19
+ ignore = true;
16
20
  rl.input.removeListener('keypress', handler);
17
21
  };
18
22
  }, []);
@@ -1,13 +1,5 @@
1
1
  import type { InquirerReadline } from '@inquirer/type';
2
- type HookStore = {
3
- rl: InquirerReadline;
4
- hooks: any[];
5
- hooksCleanup: Array<void | (() => void)>;
6
- hooksEffect: Array<() => void>;
7
- index: number;
8
- handleChange: () => void;
9
- };
10
- export declare function withHooks(rl: InquirerReadline, cb: (store: HookStore) => void): void;
2
+ export declare function withHooks(rl: InquirerReadline, cb: (cycle: (render: () => void) => void) => void): void;
11
3
  export declare function readline(): InquirerReadline;
12
4
  export declare function withUpdates<T extends (...args: any) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
13
5
  type SetPointer<Value> = {
@@ -26,5 +18,6 @@ export declare function handleChange(): void;
26
18
  export declare const effectScheduler: {
27
19
  queue(cb: (readline: InquirerReadline) => void): void;
28
20
  run(): void;
21
+ clearAll(): void;
29
22
  };
30
23
  export {};
@@ -1,4 +1,5 @@
1
1
  import * as readline from 'node:readline';
2
+ import { AsyncResource } from 'node:async_hooks';
2
3
  import { CancelablePromise } from '@inquirer/type';
3
4
  import MuteStream from 'mute-stream';
4
5
  import { onExit as onSignalExit } from 'signal-exit';
@@ -20,7 +21,7 @@ export function createPrompt(view) {
20
21
  const screen = new ScreenManager(rl);
21
22
  let cancel = () => { };
22
23
  const answer = new CancelablePromise((resolve, reject) => {
23
- withHooks(rl, (store) => {
24
+ withHooks(rl, (cycle) => {
24
25
  function checkCursorPos() {
25
26
  screen.checkCursorPos();
26
27
  }
@@ -28,15 +29,16 @@ export function createPrompt(view) {
28
29
  onExit();
29
30
  reject(new ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
30
31
  });
31
- function onExit() {
32
+ const hooksCleanup = AsyncResource.bind(() => {
32
33
  try {
33
- store.hooksCleanup.forEach((cleanFn) => {
34
- cleanFn?.();
35
- });
34
+ effectScheduler.clearAll();
36
35
  }
37
36
  catch (error) {
38
37
  reject(error);
39
38
  }
39
+ });
40
+ function onExit() {
41
+ hooksCleanup();
40
42
  if (context?.clearPromptOnDone) {
41
43
  screen.clean();
42
44
  }
@@ -45,7 +47,8 @@ export function createPrompt(view) {
45
47
  }
46
48
  screen.done();
47
49
  removeExitListener();
48
- store.rl.input.removeListener('keypress', checkCursorPos);
50
+ rl.input.removeListener('keypress', checkCursorPos);
51
+ rl.removeListener('close', hooksCleanup);
49
52
  }
50
53
  cancel = () => {
51
54
  onExit();
@@ -59,8 +62,7 @@ export function createPrompt(view) {
59
62
  resolve(value);
60
63
  });
61
64
  }
62
- function workLoop() {
63
- store.index = 0;
65
+ cycle(() => {
64
66
  try {
65
67
  const nextView = view(config, done);
66
68
  const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
@@ -71,14 +73,16 @@ export function createPrompt(view) {
71
73
  onExit();
72
74
  reject(error);
73
75
  }
74
- }
75
- store.handleChange = () => workLoop();
76
- workLoop();
76
+ });
77
77
  // Re-renders only happen when the state change; but the readline cursor could change position
78
78
  // and that also requires a re-render (and a manual one because we mute the streams).
79
79
  // We set the listener after the initial workLoop to avoid a double render if render triggered
80
80
  // by a state change sets the cursor to the right position.
81
- store.rl.input.on('keypress', checkCursorPos);
81
+ rl.input.on('keypress', checkCursorPos);
82
+ // The close event triggers immediately when the user press ctrl+c. SignalExit on the other hand
83
+ // triggers after the process is done (which happens after timeouts are done triggering.)
84
+ // We triggers the hooks cleanup phase on rl `close` so active timeouts can be cleared.
85
+ rl.on('close', hooksCleanup);
82
86
  });
83
87
  });
84
88
  answer.cancel = cancel;
@@ -17,7 +17,14 @@ function createStore(rl) {
17
17
  export function withHooks(rl, cb) {
18
18
  const store = createStore(rl);
19
19
  return hookStorage.run(store, () => {
20
- cb(store);
20
+ function cycle(render) {
21
+ store.handleChange = () => {
22
+ store.index = 0;
23
+ render();
24
+ };
25
+ store.handleChange();
26
+ }
27
+ cb(cycle);
21
28
  });
22
29
  }
23
30
  // Safe getStore utility that'll return the store or throw if undefined.
@@ -92,4 +99,12 @@ export const effectScheduler = {
92
99
  store.hooksEffect.length = 0;
93
100
  })();
94
101
  },
102
+ clearAll() {
103
+ const store = getStore();
104
+ store.hooksCleanup.forEach((cleanFn) => {
105
+ cleanFn?.();
106
+ });
107
+ store.hooksEffect.length = 0;
108
+ store.hooksCleanup.length = 0;
109
+ },
95
110
  };
@@ -5,11 +5,15 @@ export function useKeypress(userHandler) {
5
5
  const signal = useRef(userHandler);
6
6
  signal.current = userHandler;
7
7
  useEffect((rl) => {
8
+ let ignore = false;
8
9
  const handler = withUpdates((_input, event) => {
10
+ if (ignore)
11
+ return;
9
12
  signal.current(event, rl);
10
13
  });
11
14
  rl.input.on('keypress', handler);
12
15
  return () => {
16
+ ignore = true;
13
17
  rl.input.removeListener('keypress', handler);
14
18
  };
15
19
  }, []);
@@ -1,13 +1,5 @@
1
1
  import type { InquirerReadline } from '@inquirer/type';
2
- type HookStore = {
3
- rl: InquirerReadline;
4
- hooks: any[];
5
- hooksCleanup: Array<void | (() => void)>;
6
- hooksEffect: Array<() => void>;
7
- index: number;
8
- handleChange: () => void;
9
- };
10
- export declare function withHooks(rl: InquirerReadline, cb: (store: HookStore) => void): void;
2
+ export declare function withHooks(rl: InquirerReadline, cb: (cycle: (render: () => void) => void) => void): void;
11
3
  export declare function readline(): InquirerReadline;
12
4
  export declare function withUpdates<T extends (...args: any) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
13
5
  type SetPointer<Value> = {
@@ -26,5 +18,6 @@ export declare function handleChange(): void;
26
18
  export declare const effectScheduler: {
27
19
  queue(cb: (readline: InquirerReadline) => void): void;
28
20
  run(): void;
21
+ clearAll(): void;
29
22
  };
30
23
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "9.0.6",
3
+ "version": "9.0.8",
4
4
  "engines": {
5
5
  "node": ">=18"
6
6
  },
@@ -60,7 +60,7 @@
60
60
  "@inquirer/figures": "^1.0.5",
61
61
  "@inquirer/type": "^1.5.1",
62
62
  "@types/mute-stream": "^0.0.4",
63
- "@types/node": "^20.14.13",
63
+ "@types/node": "^22.0.0",
64
64
  "@types/wrap-ansi": "^3.0.0",
65
65
  "ansi-escapes": "^4.3.2",
66
66
  "cli-spinners": "^2.9.2",
@@ -72,7 +72,7 @@
72
72
  "yoctocolors-cjs": "^2.1.2"
73
73
  },
74
74
  "devDependencies": {
75
- "@inquirer/testing": "^2.1.29"
75
+ "@inquirer/testing": "^2.1.30"
76
76
  },
77
77
  "scripts": {
78
78
  "tsc": "yarn run tsc:esm && yarn run tsc:cjs",
@@ -96,5 +96,5 @@
96
96
  }
97
97
  },
98
98
  "sideEffects": false,
99
- "gitHead": "7a061f13e053dd64ef7373b08e8a10130131ea59"
99
+ "gitHead": "8be69de6107bed7d85a7f2bfe99353d106d9c3bf"
100
100
  }