Skip to content

Commit 190892b

Browse files
authored
Merge pull request #474 from aws/fix/keyup-performance-deep-clone
fix: cache hasImageCommand to avoid deep-cloning contextCommands on every keyup
2 parents ef8232b + 7adac85 commit 190892b

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/components/chat-item/prompt-input/prompt-text-input.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class PromptTextInput {
3535
private contextTooltip: Overlay | null;
3636
private contextTooltipTimeout: ReturnType<typeof setTimeout>;
3737
private mutationObserver: MutationObserver | null = null;
38+
private hasImageCommand: boolean = false;
3839

3940
constructor (props: PromptTextInputProps) {
4041
this.props = props;
@@ -75,13 +76,7 @@ export class PromptTextInput {
7576
keyup: (e: KeyboardEvent) => {
7677
this.lastCursorIndex = this.updateCursorPos();
7778

78-
// Check if image command exists in context commands to make the feature consistent
79-
const contextCommands = MynahUITabsStore.getInstance().getTabDataStore(this.props.tabId).getValue('contextCommands') as QuickActionCommandGroup[] | undefined;
80-
const hasImageCommand = contextCommands?.some(group =>
81-
group.commands.some(cmd => cmd.command.toLowerCase() === 'image')
82-
);
83-
84-
if (hasImageCommand ?? false) {
79+
if (this.hasImageCommand) {
8580
const text = this.promptTextInput.textContent ?? '';
8681
if (text.includes(IMAGE_CONTEXT_SELECT_KEYWORD)) {
8782
// Dispatch event to open file system
@@ -175,6 +170,18 @@ export class PromptTextInput {
175170
// Set up MutationObserver to detect context span removals
176171
this.setupContextRemovalObserver();
177172

173+
// Cache whether image command exists to avoid deep-cloning contextCommands on every keyup
174+
const contextCommandsRaw = MynahUITabsStore.getInstance().getTabDataStore(this.props.tabId).getValue('contextCommands');
175+
this.hasImageCommand = Array.isArray(contextCommandsRaw) && (contextCommandsRaw as QuickActionCommandGroup[]).some(group =>
176+
group.commands.some(cmd => cmd.command.toLowerCase() === 'image')
177+
);
178+
179+
MynahUITabsStore.getInstance().getTabDataStore(this.props.tabId).subscribe('contextCommands', (contextCommands: QuickActionCommandGroup[]) => {
180+
this.hasImageCommand = Array.isArray(contextCommands) && contextCommands.some(group =>
181+
group.commands.some(cmd => cmd.command.toLowerCase() === 'image')
182+
);
183+
});
184+
178185
MynahUITabsStore.getInstance().getTabDataStore(this.props.tabId).subscribe('promptInputDisabledState', (isDisabled: boolean) => {
179186
if (isDisabled) {
180187
this.promptTextInput.setAttribute('disabled', 'disabled');

0 commit comments

Comments
 (0)