inquirerer 2.2.0 → 2.2.1

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/esm/keypress.js CHANGED
@@ -12,38 +12,78 @@ export const KEY_CODES = {
12
12
  BACKSPACE: '\x7f', // Commonly used BACKSPACE key in Unix-like systems
13
13
  BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
14
14
  };
15
+ const sharedInputStates = new WeakMap();
16
+ function getOrCreateSharedState(input, proc) {
17
+ let state = sharedInputStates.get(input);
18
+ if (!state) {
19
+ const dataHandler = (key) => {
20
+ const currentState = sharedInputStates.get(input);
21
+ if (!currentState)
22
+ return;
23
+ for (const instance of currentState.instances) {
24
+ if (instance.isActive()) {
25
+ instance.handleKey(key);
26
+ }
27
+ }
28
+ if (key === KEY_CODES.CTRL_C) {
29
+ proc.exit(0);
30
+ }
31
+ };
32
+ state = {
33
+ dataHandler,
34
+ instances: new Set(),
35
+ rawModeSet: false
36
+ };
37
+ sharedInputStates.set(input, state);
38
+ input.on('data', dataHandler);
39
+ }
40
+ return state;
41
+ }
42
+ function removeFromSharedState(input, instance) {
43
+ const state = sharedInputStates.get(input);
44
+ if (!state)
45
+ return;
46
+ state.instances.delete(instance);
47
+ if (state.instances.size === 0) {
48
+ input.removeListener('data', state.dataHandler);
49
+ sharedInputStates.delete(input);
50
+ if (state.rawModeSet && typeof input.setRawMode === 'function') {
51
+ input.setRawMode(false);
52
+ }
53
+ }
54
+ }
15
55
  export class TerminalKeypress {
16
56
  listeners = {};
17
57
  active = true;
18
58
  noTty;
19
59
  input;
20
60
  proc;
21
- dataHandler = null;
61
+ destroyed = false;
22
62
  constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
23
63
  this.noTty = noTty;
24
64
  this.input = input;
25
65
  this.proc = proc;
26
- // Don't set raw mode yet; let resume() handle it when necessary
27
66
  if (this.isTTY()) {
28
67
  this.input.resume();
29
68
  this.input.setEncoding('utf8');
30
69
  }
31
- this.setupListeners();
70
+ this.registerWithSharedState();
71
+ }
72
+ registerWithSharedState() {
73
+ const state = getOrCreateSharedState(this.input, this.proc);
74
+ state.instances.add(this);
32
75
  }
33
76
  isTTY() {
34
77
  return !this.noTty;
35
78
  }
36
- setupListeners() {
37
- this.dataHandler = (key) => {
38
- if (!this.active)
39
- return;
40
- const handlers = this.listeners[key];
41
- handlers?.forEach(handler => handler());
42
- if (key === KEY_CODES.CTRL_C) { // Ctrl+C
43
- this.proc.exit(0);
44
- }
45
- };
46
- this.input.on('data', this.dataHandler);
79
+ isActive() {
80
+ return this.active && !this.destroyed;
81
+ }
82
+ handleKey(key) {
83
+ if (!this.active || this.destroyed)
84
+ return;
85
+ const handlers = this.listeners[key];
86
+ handlers?.forEach(handler => handler());
47
87
  }
48
88
  on(key, callback) {
49
89
  if (!this.listeners[key]) {
@@ -64,21 +104,31 @@ export class TerminalKeypress {
64
104
  }
65
105
  pause() {
66
106
  this.active = false;
107
+ this.clearHandlers();
67
108
  }
68
109
  resume() {
69
110
  this.active = true;
70
111
  if (this.isTTY() && typeof this.input.setRawMode === 'function') {
71
112
  this.input.setRawMode(true);
113
+ const state = sharedInputStates.get(this.input);
114
+ if (state) {
115
+ state.rawModeSet = true;
116
+ }
72
117
  }
73
118
  }
74
119
  destroy() {
75
- if (typeof this.input.setRawMode === 'function') {
76
- this.input.setRawMode(false);
77
- }
78
- this.input.pause();
79
- if (this.dataHandler) {
80
- this.input.removeListener('data', this.dataHandler);
81
- this.dataHandler = null;
120
+ if (this.destroyed)
121
+ return;
122
+ this.destroyed = true;
123
+ this.active = false;
124
+ this.clearHandlers();
125
+ removeFromSharedState(this.input, this);
126
+ const state = sharedInputStates.get(this.input);
127
+ if (!state || state.instances.size === 0) {
128
+ if (typeof this.input.setRawMode === 'function') {
129
+ this.input.setRawMode(false);
130
+ }
131
+ this.input.pause();
82
132
  }
83
133
  }
84
134
  }
package/keypress.d.ts CHANGED
@@ -20,10 +20,12 @@ export declare class TerminalKeypress {
20
20
  private noTty;
21
21
  private input;
22
22
  private proc;
23
- private dataHandler;
23
+ private destroyed;
24
24
  constructor(noTty?: boolean, input?: Readable, proc?: ProcessWrapper);
25
+ private registerWithSharedState;
25
26
  isTTY(): boolean;
26
- private setupListeners;
27
+ isActive(): boolean;
28
+ handleKey(key: string): void;
27
29
  on(key: string, callback: KeyHandler): void;
28
30
  off(key: string, callback: KeyHandler): void;
29
31
  clearHandlers(): void;
package/keypress.js CHANGED
@@ -15,38 +15,78 @@ exports.KEY_CODES = {
15
15
  BACKSPACE: '\x7f', // Commonly used BACKSPACE key in Unix-like systems
16
16
  BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
17
17
  };
18
+ const sharedInputStates = new WeakMap();
19
+ function getOrCreateSharedState(input, proc) {
20
+ let state = sharedInputStates.get(input);
21
+ if (!state) {
22
+ const dataHandler = (key) => {
23
+ const currentState = sharedInputStates.get(input);
24
+ if (!currentState)
25
+ return;
26
+ for (const instance of currentState.instances) {
27
+ if (instance.isActive()) {
28
+ instance.handleKey(key);
29
+ }
30
+ }
31
+ if (key === exports.KEY_CODES.CTRL_C) {
32
+ proc.exit(0);
33
+ }
34
+ };
35
+ state = {
36
+ dataHandler,
37
+ instances: new Set(),
38
+ rawModeSet: false
39
+ };
40
+ sharedInputStates.set(input, state);
41
+ input.on('data', dataHandler);
42
+ }
43
+ return state;
44
+ }
45
+ function removeFromSharedState(input, instance) {
46
+ const state = sharedInputStates.get(input);
47
+ if (!state)
48
+ return;
49
+ state.instances.delete(instance);
50
+ if (state.instances.size === 0) {
51
+ input.removeListener('data', state.dataHandler);
52
+ sharedInputStates.delete(input);
53
+ if (state.rawModeSet && typeof input.setRawMode === 'function') {
54
+ input.setRawMode(false);
55
+ }
56
+ }
57
+ }
18
58
  class TerminalKeypress {
19
59
  listeners = {};
20
60
  active = true;
21
61
  noTty;
22
62
  input;
23
63
  proc;
24
- dataHandler = null;
64
+ destroyed = false;
25
65
  constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
26
66
  this.noTty = noTty;
27
67
  this.input = input;
28
68
  this.proc = proc;
29
- // Don't set raw mode yet; let resume() handle it when necessary
30
69
  if (this.isTTY()) {
31
70
  this.input.resume();
32
71
  this.input.setEncoding('utf8');
33
72
  }
34
- this.setupListeners();
73
+ this.registerWithSharedState();
74
+ }
75
+ registerWithSharedState() {
76
+ const state = getOrCreateSharedState(this.input, this.proc);
77
+ state.instances.add(this);
35
78
  }
36
79
  isTTY() {
37
80
  return !this.noTty;
38
81
  }
39
- setupListeners() {
40
- this.dataHandler = (key) => {
41
- if (!this.active)
42
- return;
43
- const handlers = this.listeners[key];
44
- handlers?.forEach(handler => handler());
45
- if (key === exports.KEY_CODES.CTRL_C) { // Ctrl+C
46
- this.proc.exit(0);
47
- }
48
- };
49
- this.input.on('data', this.dataHandler);
82
+ isActive() {
83
+ return this.active && !this.destroyed;
84
+ }
85
+ handleKey(key) {
86
+ if (!this.active || this.destroyed)
87
+ return;
88
+ const handlers = this.listeners[key];
89
+ handlers?.forEach(handler => handler());
50
90
  }
51
91
  on(key, callback) {
52
92
  if (!this.listeners[key]) {
@@ -67,21 +107,31 @@ class TerminalKeypress {
67
107
  }
68
108
  pause() {
69
109
  this.active = false;
110
+ this.clearHandlers();
70
111
  }
71
112
  resume() {
72
113
  this.active = true;
73
114
  if (this.isTTY() && typeof this.input.setRawMode === 'function') {
74
115
  this.input.setRawMode(true);
116
+ const state = sharedInputStates.get(this.input);
117
+ if (state) {
118
+ state.rawModeSet = true;
119
+ }
75
120
  }
76
121
  }
77
122
  destroy() {
78
- if (typeof this.input.setRawMode === 'function') {
79
- this.input.setRawMode(false);
80
- }
81
- this.input.pause();
82
- if (this.dataHandler) {
83
- this.input.removeListener('data', this.dataHandler);
84
- this.dataHandler = null;
123
+ if (this.destroyed)
124
+ return;
125
+ this.destroyed = true;
126
+ this.active = false;
127
+ this.clearHandlers();
128
+ removeFromSharedState(this.input, this);
129
+ const state = sharedInputStates.get(this.input);
130
+ if (!state || state.instances.size === 0) {
131
+ if (typeof this.input.setRawMode === 'function') {
132
+ this.input.setRawMode(false);
133
+ }
134
+ this.input.pause();
85
135
  }
86
136
  }
87
137
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
6
6
  "main": "index.js",
@@ -41,5 +41,5 @@
41
41
  "clean-ansi": "0.1.5",
42
42
  "makage": "0.1.8"
43
43
  },
44
- "gitHead": "4daa10528afcb41fa99f6e94646ce40354121794"
44
+ "gitHead": "ad34404aab89a1f92d11de28f908ce80adcb7d72"
45
45
  }