@@ -12,14 +12,13 @@ import { IHostsListObject } from '@root/common/data'
12
12
import events from '@root/common/events'
13
13
import { IFindShowSourceParam } from '@root/common/types'
14
14
import wait from '@root/common/utils/wait'
15
+ import { useDebounceFn } from 'ahooks'
15
16
import clsx from 'clsx'
16
17
import CodeMirror from 'codemirror'
17
18
import 'codemirror/addon/comment/comment'
18
19
import 'codemirror/addon/selection/mark-selection'
19
- import lodash from 'lodash'
20
20
import React , { useEffect , useRef , useState } from 'react'
21
21
import modeHosts from './cm_hl'
22
- // import 'codemirror/lib/codemirror.css'
23
22
import './codemirror.less'
24
23
import styles from './HostsEditor.less'
25
24
@@ -58,7 +57,7 @@ const HostsEditor = (props: Props) => {
58
57
}
59
58
60
59
useEffect ( ( ) => {
61
- loadContent ( )
60
+ loadContent ( ) . catch ( ( e ) => console . error ( e ) )
62
61
} , [ hosts_id , cm_editor ] )
63
62
64
63
useEffect ( ( ) => {
@@ -70,12 +69,15 @@ const HostsEditor = (props: Props) => {
70
69
setIsReadOnly ( isReadOnly ( hosts ) )
71
70
} , [ hosts ] )
72
71
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
+ )
79
81
80
82
const onChange = ( content : string ) => {
81
83
setContent ( content )
@@ -134,7 +136,7 @@ const HostsEditor = (props: Props) => {
134
136
135
137
useEffect ( ( ) => {
136
138
if ( find_params && find_params . item_id === hosts . id ) {
137
- setSelection ( find_params )
139
+ setSelection ( find_params ) . catch ( ( e ) => console . error ( e ) )
138
140
}
139
141
} , [ hosts , find_params ] )
140
142
@@ -156,7 +158,7 @@ const HostsEditor = (props: Props) => {
156
158
events . hosts_refreshed ,
157
159
( h : IHostsListObject ) => {
158
160
if ( hosts . id !== '0' && h . id !== hosts . id ) return
159
- loadContent ( )
161
+ loadContent ( ) . catch ( ( e ) => console . error ( e ) )
160
162
} ,
161
163
[ hosts , hosts_data , cm_editor ] ,
162
164
)
@@ -165,7 +167,7 @@ const HostsEditor = (props: Props) => {
165
167
events . hosts_refreshed_by_id ,
166
168
( id : string ) => {
167
169
if ( hosts . id !== '0' && id !== hosts . id ) return
168
- loadContent ( )
170
+ loadContent ( ) . catch ( ( e ) => console . error ( e ) )
169
171
} ,
170
172
[ hosts , hosts_data , cm_editor ] ,
171
173
)
@@ -179,7 +181,7 @@ const HostsEditor = (props: Props) => {
179
181
events . set_hosts_on_status ,
180
182
( ) => {
181
183
if ( hosts . id === '0' ) {
182
- loadContent ( )
184
+ loadContent ( ) . catch ( ( e ) => console . error ( e ) )
183
185
}
184
186
} ,
185
187
[ hosts , cm_editor ] ,
@@ -221,7 +223,7 @@ const HostsEditor = (props: Props) => {
221
223
return
222
224
}
223
225
224
- setSelection ( params )
226
+ setSelection ( params ) . catch ( ( e ) => console . error ( e ) )
225
227
} ,
226
228
[ hosts , cm_editor ] ,
227
229
)
0 commit comments