Skip to content

Commit c5e8e2e

Browse files
committed
perf(mobile): sidebar stops re-registering shortcuts every render
The sidebar's list derivations stayed live across later hook calls, so the React Compiler pruned their cache scope. LegendList got new data and the thread-jump shortcuts re-registered on every render, which re-rendered the app-root keyboard provider and sent native a new enabledCommands array. Run every hook before the list derivations, with the filter and project-scope derivation owned by a small custom hook, and register thread-jump shortcuts once, reading the latest list through useEffectEvent.
1 parent 4af6618 commit c5e8e2e

5 files changed

Lines changed: 235 additions & 164 deletions

File tree

‎apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,34 @@ const handlers = new Map<HardwareKeyboardCommand, Set<CommandHandler>>();
2323
const registrationListeners = new Set<() => void>();
2424
let registrationVersion = 0;
2525

26-
export function useHardwareKeyboardCommand(
26+
export function registerHardwareKeyboardCommand(
2727
command: HardwareKeyboardCommand | ReadonlyArray<HardwareKeyboardCommand>,
2828
handler: CommandHandler,
29-
): void {
30-
useEffect(() => {
31-
const commands = typeof command === "string" ? [command] : command;
29+
): () => void {
30+
const commands = typeof command === "string" ? [command] : command;
31+
for (const command of commands) {
32+
const commandHandlers = handlers.get(command) ?? new Set<CommandHandler>();
33+
commandHandlers.add(handler);
34+
handlers.set(command, commandHandlers);
35+
}
36+
registrationVersion += 1;
37+
registrationListeners.forEach((listener) => listener());
38+
return () => {
3239
for (const command of commands) {
33-
const commandHandlers = handlers.get(command) ?? new Set<CommandHandler>();
34-
commandHandlers.add(handler);
35-
handlers.set(command, commandHandlers);
40+
const commandHandlers = handlers.get(command);
41+
commandHandlers?.delete(handler);
42+
if (commandHandlers?.size === 0) handlers.delete(command);
3643
}
3744
registrationVersion += 1;
3845
registrationListeners.forEach((listener) => listener());
39-
return () => {
40-
for (const command of commands) {
41-
const commandHandlers = handlers.get(command);
42-
commandHandlers?.delete(handler);
43-
if (commandHandlers?.size === 0) handlers.delete(command);
44-
}
45-
registrationVersion += 1;
46-
registrationListeners.forEach((listener) => listener());
47-
};
48-
}, [command, handler]);
46+
};
47+
}
48+
49+
export function useHardwareKeyboardCommand(
50+
command: HardwareKeyboardCommand | ReadonlyArray<HardwareKeyboardCommand>,
51+
handler: CommandHandler,
52+
): void {
53+
useEffect(() => registerHardwareKeyboardCommand(command, handler), [command, handler]);
4954
}
5055

5156
export function getRegisteredHardwareKeyboardCommands(): ReadonlySet<HardwareKeyboardCommand> {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2+
import { act } from "react";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
5+
6+
import type { ThreadListV2ListItem } from "../threads/threadListV2";
7+
import {
8+
dispatchHardwareKeyboardCommand,
9+
getHardwareKeyboardCommandRegistrationVersion,
10+
} from "./hardwareKeyboardCommands";
11+
import { useThreadJumpShortcuts } from "./threadKeyboardShortcuts";
12+
13+
function threadItem(id: string): ThreadListV2ListItem {
14+
const thread = { environmentId: "environment-1", id } as unknown as EnvironmentThreadShell;
15+
return {
16+
type: "v2-thread",
17+
key: id,
18+
item: { thread, variant: "card", snoozed: false, pinned: false, isLast: false },
19+
snoozeWakeLabelText: undefined,
20+
timeLabel: "",
21+
snoozePresetMinute: undefined,
22+
showTrailingDivider: false,
23+
hasQueuedMessages: false,
24+
canMoveUp: false,
25+
canMoveDown: false,
26+
};
27+
}
28+
29+
function Probe(props: {
30+
readonly items: ReadonlyArray<ThreadListV2ListItem>;
31+
readonly onSelectThread: (thread: EnvironmentThreadShell) => void;
32+
}) {
33+
useThreadJumpShortcuts(props.items, props.onSelectThread);
34+
return null;
35+
}
36+
37+
let root: Root | null = null;
38+
39+
afterEach(() => {
40+
act(() => root?.unmount());
41+
root = null;
42+
});
43+
44+
describe("useThreadJumpShortcuts", () => {
45+
it("registers once and jumps within the latest list", () => {
46+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
47+
root = createRoot(document.createElement("div"));
48+
act(() => root!.render(<Probe items={[threadItem("first")]} onSelectThread={vi.fn()} />));
49+
const registrationVersion = getHardwareKeyboardCommandRegistrationVersion();
50+
51+
const onSelectThread = vi.fn();
52+
for (const id of ["second", "third"]) {
53+
act(() => root!.render(<Probe items={[threadItem(id)]} onSelectThread={onSelectThread} />));
54+
}
55+
56+
expect(getHardwareKeyboardCommandRegistrationVersion()).toBe(registrationVersion);
57+
expect(dispatchHardwareKeyboardCommand("thread.jump.1")).toBe(true);
58+
expect(onSelectThread).toHaveBeenCalledExactlyOnceWith(
59+
expect.objectContaining({ id: "third" }),
60+
);
61+
});
62+
});

‎apps/mobile/src/features/keyboard/threadKeyboardShortcuts.ts‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
22
import { THREAD_JUMP_KEYBINDING_COMMANDS } from "@t3tools/contracts";
3-
import { useCallback } from "react";
3+
import { useEffect, useEffectEvent } from "react";
44

55
import type { ThreadListV2ListItem } from "../threads/threadListV2";
66
import {
7-
useHardwareKeyboardCommand,
7+
registerHardwareKeyboardCommand,
88
type HardwareKeyboardCommand,
99
} from "./hardwareKeyboardCommands";
1010

@@ -31,13 +31,16 @@ export function useThreadJumpShortcuts(
3131
items: ReadonlyArray<ThreadShortcutListItem>,
3232
onSelectThread: (thread: EnvironmentThreadShell) => void,
3333
) {
34-
const jumpToThread = useCallback(
35-
(command: HardwareKeyboardCommand) => {
36-
const thread = threadJumpTarget(items, command);
37-
if (thread !== null) onSelectThread(thread);
38-
return true;
39-
},
40-
[items, onSelectThread],
34+
const jumpToThread = useEffectEvent((command: HardwareKeyboardCommand) => {
35+
const thread = threadJumpTarget(items, command);
36+
if (thread !== null) onSelectThread(thread);
37+
});
38+
useEffect(
39+
() =>
40+
registerHardwareKeyboardCommand(THREAD_JUMP_KEYBINDING_COMMANDS, (command) => {
41+
jumpToThread(command);
42+
return true;
43+
}),
44+
[],
4145
);
42-
useHardwareKeyboardCommand(THREAD_JUMP_KEYBINDING_COMMANDS, jumpToThread);
4346
}

0 commit comments

Comments
 (0)