Skip to content

Commit e9acd40

Browse files
committed
Update debounce functions.
1 parent 8bc87c6 commit e9acd40

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

src/renderer/components/Editor/HostsEditor.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import { IHostsListObject } from '@root/common/data'
1212
import events from '@root/common/events'
1313
import { IFindShowSourceParam } from '@root/common/types'
1414
import wait from '@root/common/utils/wait'
15+
import { useDebounceFn } from 'ahooks'
1516
import clsx from 'clsx'
1617
import CodeMirror from 'codemirror'
1718
import 'codemirror/addon/comment/comment'
1819
import 'codemirror/addon/selection/mark-selection'
19-
import lodash from 'lodash'
2020
import React, { useEffect, useRef, useState } from 'react'
2121
import modeHosts from './cm_hl'
22-
// import 'codemirror/lib/codemirror.css'
2322
import './codemirror.less'
2423
import styles from './HostsEditor.less'
2524

@@ -58,7 +57,7 @@ const HostsEditor = (props: Props) => {
5857
}
5958

6059
useEffect(() => {
61-
loadContent()
60+
loadContent().catch((e) => console.error(e))
6261
}, [hosts_id, cm_editor])
6362

6463
useEffect(() => {
@@ -70,12 +69,15 @@ const HostsEditor = (props: Props) => {
7069
setIsReadOnly(isReadOnly(hosts))
7170
}, [hosts])
7271

73-
const toSave = lodash.debounce((id: string, content: string) => {
74-
actions
75-
.setHostsContent(id, content)
76-
.then(() => agent.broadcast(events.hosts_content_changed, id))
77-
.catch((e) => console.error(e))
78-
}, 1000)
72+
const { run: toSave } = useDebounceFn(
73+
(id: string, content: string) => {
74+
actions
75+
.setHostsContent(id, content)
76+
.then(() => agent.broadcast(events.hosts_content_changed, id))
77+
.catch((e) => console.error(e))
78+
},
79+
{ wait: 1000 },
80+
)
7981

8082
const onChange = (content: string) => {
8183
setContent(content)
@@ -134,7 +136,7 @@ const HostsEditor = (props: Props) => {
134136

135137
useEffect(() => {
136138
if (find_params && find_params.item_id === hosts.id) {
137-
setSelection(find_params)
139+
setSelection(find_params).catch((e) => console.error(e))
138140
}
139141
}, [hosts, find_params])
140142

@@ -156,7 +158,7 @@ const HostsEditor = (props: Props) => {
156158
events.hosts_refreshed,
157159
(h: IHostsListObject) => {
158160
if (hosts.id !== '0' && h.id !== hosts.id) return
159-
loadContent()
161+
loadContent().catch((e) => console.error(e))
160162
},
161163
[hosts, hosts_data, cm_editor],
162164
)
@@ -165,7 +167,7 @@ const HostsEditor = (props: Props) => {
165167
events.hosts_refreshed_by_id,
166168
(id: string) => {
167169
if (hosts.id !== '0' && id !== hosts.id) return
168-
loadContent()
170+
loadContent().catch((e) => console.error(e))
169171
},
170172
[hosts, hosts_data, cm_editor],
171173
)
@@ -179,7 +181,7 @@ const HostsEditor = (props: Props) => {
179181
events.set_hosts_on_status,
180182
() => {
181183
if (hosts.id === '0') {
182-
loadContent()
184+
loadContent().catch((e) => console.error(e))
183185
}
184186
},
185187
[hosts, cm_editor],
@@ -221,7 +223,7 @@ const HostsEditor = (props: Props) => {
221223
return
222224
}
223225

224-
setSelection(params)
226+
setSelection(params).catch((e) => console.error(e))
225227
},
226228
[hosts, cm_editor],
227229
)

src/renderer/pages/find.tsx

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
IFindPosition,
3131
IFindShowSourceParam,
3232
} from '@root/common/types'
33-
import { useDebounce } from 'ahooks'
33+
import { useDebounce, useDebounceFn } from 'ahooks'
3434
import clsx from 'clsx'
3535
import lodash from 'lodash'
3636
import React, { useEffect, useRef, useState } from 'react'
@@ -146,30 +146,33 @@ const find = (props: Props) => {
146146
setFindPositions(positions_show)
147147
}
148148

149-
const doFind = lodash.debounce(async (v: string) => {
150-
console.log('find by:', v)
151-
if (!v) {
152-
setFindResult([])
153-
return
154-
}
149+
const { run: doFind } = useDebounceFn(
150+
async (v: string) => {
151+
console.log('find by:', v)
152+
if (!v) {
153+
setFindResult([])
154+
return
155+
}
155156

156-
setIsSearching(true)
157-
let result = await actions.findBy(v, {
158-
is_regexp,
159-
is_ignore_case,
160-
})
161-
setCurrentResultIdx(0)
162-
setlastScrollResultIdx(0)
163-
setFindResult(result)
164-
parsePositionShow(result)
165-
setIsSearching(false)
166-
167-
await actions.findAddHistory({
168-
value: v,
169-
is_regexp,
170-
is_ignore_case,
171-
})
172-
}, 500)
157+
setIsSearching(true)
158+
let result = await actions.findBy(v, {
159+
is_regexp,
160+
is_ignore_case,
161+
})
162+
setCurrentResultIdx(0)
163+
setlastScrollResultIdx(0)
164+
setFindResult(result)
165+
parsePositionShow(result)
166+
setIsSearching(false)
167+
168+
await actions.findAddHistory({
169+
value: v,
170+
is_regexp,
171+
is_ignore_case,
172+
})
173+
},
174+
{ wait: 500 },
175+
)
173176

174177
const toShowSource = async (result_item: IFindPositionShow) => {
175178
// console.log(result_item)
@@ -262,7 +265,7 @@ const find = (props: Props) => {
262265
scrollIntoView(el.current, {
263266
behavior: 'smooth',
264267
scrollMode: 'if-needed',
265-
})
268+
}).catch((e) => console.error(e))
266269
}
267270
}, [el, current_result_idx, last_scroll_result_idx])
268271

0 commit comments

Comments
 (0)