Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.

Commit f77faea

Browse files
committed
Use react memo instead of inline custom memoize
1 parent 833e91e commit f77faea

3 files changed

Lines changed: 28 additions & 48 deletions

File tree

lib/shared/src/lexicalEditor/editorState.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { type ContextItem, ContextItemSource } from '../codebase-context/message
1111
import type { RangeData } from '../common/range'
1212
import { displayPath } from '../editor/displayPath'
1313
import type { PromptString } from '../prompt/prompt-string'
14-
import { memoize } from '../utils'
1514
import { AT_MENTION_SERIALIZED_PREFIX, deserializeParagraph } from './atMentionsSerializer'
1615
import {
1716
CONTEXT_ITEM_MENTION_NODE_TYPE,
@@ -148,36 +147,36 @@ export function serializedPromptEditorStateFromText(text: string): SerializedPro
148147
}
149148
}
150149

151-
export const serializedPromptEditorStateFromChatMessage = memoize(
152-
(chatMessage: ChatMessage): SerializedPromptEditorState => {
153-
function isCompatibleVersionEditorState(value: unknown): value is SerializedPromptEditorState {
154-
if (!value) {
155-
return false
156-
}
157-
158-
const editorState = value as SerializedPromptEditorState
159-
160-
// We can read this if the version of the serialized text is compatible
161-
// or its minimum version is compatible.
162-
return (
163-
SUPPORTED_READER_VERSIONS.includes(editorState.v) ||
164-
SUPPORTED_READER_VERSIONS.includes(editorState.minReaderV ?? DEFAULT_MIN_READER_V)
165-
)
150+
export const serializedPromptEditorStateFromChatMessage = (
151+
chatMessage: ChatMessage
152+
): SerializedPromptEditorState => {
153+
function isCompatibleVersionEditorState(value: unknown): value is SerializedPromptEditorState {
154+
if (!value) {
155+
return false
166156
}
167157

168-
if (isCompatibleVersionEditorState(chatMessage.editorState)) {
169-
return chatMessage.editorState
170-
}
158+
const editorState = value as SerializedPromptEditorState
171159

172-
// Fall back to using plain text for chat messages that don't have a serialized Lexical editor
173-
// state that we recognize.
174-
//
175-
// It would be smoother to automatically import or convert textual @-mentions to the Lexical
176-
// mention nodes, but that would add a lot of extra complexity for the relatively rare use case
177-
// of editing old messages in your chat history.
178-
return serializedPromptEditorStateFromText(chatMessage.text ? chatMessage.text.toString() : '')
160+
// We can read this if the version of the serialized text is compatible
161+
// or its minimum version is compatible.
162+
return (
163+
SUPPORTED_READER_VERSIONS.includes(editorState.v) ||
164+
SUPPORTED_READER_VERSIONS.includes(editorState.minReaderV ?? DEFAULT_MIN_READER_V)
165+
)
179166
}
180-
)
167+
168+
if (isCompatibleVersionEditorState(chatMessage.editorState)) {
169+
return chatMessage.editorState
170+
}
171+
172+
// Fall back to using plain text for chat messages that don't have a serialized Lexical editor
173+
// state that we recognize.
174+
//
175+
// It would be smoother to automatically import or convert textual @-mentions to the Lexical
176+
// mention nodes, but that would add a lot of extra complexity for the relatively rare use case
177+
// of editing old messages in your chat history.
178+
return serializedPromptEditorStateFromText(chatMessage.text ? chatMessage.text.toString() : '')
179+
}
181180

182181
export function contextItemsFromPromptEditorValue(
183182
state: SerializedPromptEditorState

lib/shared/src/utils.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import isEqual from 'lodash/isEqual'
21
import { logError } from './logger'
32

43
type PromiseResolverFn<T, E> = (value?: T, err?: E) => void
@@ -188,21 +187,3 @@ export type PartialDeep<T> = {
188187
? PartialDeep<T[P]>
189188
: T[P]
190189
}
191-
192-
export function memoize<T extends (...args: any[]) => any>(
193-
func: T
194-
): (...args: Parameters<T>) => ReturnType<T> {
195-
let lastArguments: any[] | null = null
196-
let lastCalculatedValue: ReturnType<T> | null = null
197-
198-
return (...args: Parameters<T>): ReturnType<T> => {
199-
if (isEqual(lastArguments, args)) {
200-
return lastCalculatedValue as ReturnType<T>
201-
}
202-
203-
lastArguments = args
204-
lastCalculatedValue = func(args)
205-
206-
return lastCalculatedValue as ReturnType<T>
207-
}
208-
}

vscode/webviews/chat/cells/messageCell/human/HumanMessageCell.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ interface HumanMessageCellProps {
4848
/**
4949
* A component that displays a chat message from the human.
5050
*/
51-
export const HumanMessageCell: FC<HumanMessageCellProps> = ({ message, ...otherProps }) => {
51+
export const HumanMessageCell: FC<HumanMessageCellProps> = memo(({ message, ...otherProps }) => {
5252
// Don't render the editor if the message text is explicitly undefined or empty,
5353
// and it's been sent already and it's not the last interaction (i.e. there is a tool result response).
5454
if (
@@ -67,7 +67,7 @@ export const HumanMessageCell: FC<HumanMessageCellProps> = ({ message, ...otherP
6767
)
6868

6969
return <HumanMessageCellContent {...otherProps} initialEditorState={initialEditorState} />
70-
}
70+
})
7171

7272
type HumanMessageCellContent = {
7373
initialEditorState: SerializedPromptEditorState

0 commit comments

Comments
 (0)