Skip to content

Commit bd524c0

Browse files
fix(mobile): terminal keyboard store listens once instead of per subscriber
Each subscribe registered its own listener set over shared state and reset that state, so a screen mounting mid-repair could lose the pending didHide. The store now follows the keyboard events from import and notifies a subscriber set. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f82221d commit bd524c0

1 file changed

Lines changed: 24 additions & 25 deletions

File tree

‎apps/mobile/src/features/terminal/terminalKeyboardShowing.ts‎

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,35 @@ import { KeyboardController, KeyboardEvents } from "react-native-keyboard-contro
44
// repairs a nonanimated IME change with didHide then didShow alone: that didHide must
55
// not hide the accessory (it would clear the terminal's focus), but the didShow after
66
// it starts a new show so a dismissed accessory comes back.
7+
const subscribers = new Set<() => void>();
78
let showEpoch = 1;
89
let keyboardShowing = KeyboardController.isVisible();
9-
let hiddenWithoutWillHide = false;
10+
let hiddenSinceShow = !keyboardShowing;
11+
12+
const notify = () => {
13+
for (const subscriber of subscribers) subscriber();
14+
};
15+
const show = () => {
16+
if (hiddenSinceShow) showEpoch += 1;
17+
hiddenSinceShow = false;
18+
keyboardShowing = true;
19+
notify();
20+
};
21+
KeyboardEvents.addListener("keyboardWillShow", show);
22+
KeyboardEvents.addListener("keyboardDidShow", show);
23+
KeyboardEvents.addListener("keyboardWillHide", () => {
24+
keyboardShowing = false;
25+
hiddenSinceShow = true;
26+
notify();
27+
});
28+
KeyboardEvents.addListener("keyboardDidHide", () => {
29+
hiddenSinceShow = true;
30+
});
1031

1132
export function subscribeKeyboardShowing(onChange: () => void) {
12-
keyboardShowing = KeyboardController.isVisible();
13-
hiddenWithoutWillHide = false;
14-
const show = () => {
15-
const repaired = hiddenWithoutWillHide;
16-
hiddenWithoutWillHide = false;
17-
if (!keyboardShowing || repaired) {
18-
keyboardShowing = true;
19-
showEpoch += 1;
20-
}
21-
onChange();
22-
};
23-
const willShow = KeyboardEvents.addListener("keyboardWillShow", show);
24-
const didShow = KeyboardEvents.addListener("keyboardDidShow", show);
25-
const willHide = KeyboardEvents.addListener("keyboardWillHide", () => {
26-
keyboardShowing = false;
27-
onChange();
28-
});
29-
const didHide = KeyboardEvents.addListener("keyboardDidHide", () => {
30-
hiddenWithoutWillHide = keyboardShowing;
31-
});
33+
subscribers.add(onChange);
3234
return () => {
33-
willShow.remove();
34-
didShow.remove();
35-
willHide.remove();
36-
didHide.remove();
35+
subscribers.delete(onChange);
3736
};
3837
}
3938

0 commit comments

Comments
 (0)