Skip to content

Commit 234bfef

Browse files
feat: optimize rendering performance by deferredValue
render tree is too expensive. skip tons of update triggered by sync external
1 parent 00432fa commit 234bfef

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type Definition = {
3030
}
3131
searchEnd: {
3232
id: number
33+
inputValue: string
3334
}
3435
}
3536
child2parent: {

src/webview/hooks/useSearch.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { SgSearch } from '../postMessage'
22
import { childPort } from '../postMessage'
3-
import { useCallback, useMemo, useSyncExternalStore } from 'react'
3+
import {
4+
useCallback,
5+
useDeferredValue,
6+
useMemo,
7+
useSyncExternalStore
8+
} from 'react'
49
import { useDebounce } from 'react-use'
510

611
// id should not overflow, the MOD is large enough
@@ -36,6 +41,7 @@ childPort.onMessage('searchEnd', event => {
3641
return
3742
}
3843
searching = false
44+
queryInFlight = event.inputValue
3945
notify()
4046
})
4147

@@ -75,9 +81,11 @@ export const useSearchResult = (inputValue: string) => {
7581
postSearch(inputValue)
7682
}, [inputValue])
7783

78-
const groupedByFileSearchResult = useMemo(() => {
84+
const grouped = useMemo(() => {
7985
return [...groupBy(searchResult).entries()]
8086
}, [searchResult])
87+
// rendering tree is too expensive, useDeferredValue
88+
const groupedByFileSearchResult = useDeferredValue(grouped)
8189

8290
useDebounce(refreshSearchResult, 100, [inputValue])
8391

0 commit comments

Comments
 (0)