()
| 186 | }; |
| 187 | |
| 188 | const search = async () => { |
| 189 | if (!fileSearch.current || state.pattern === null) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | if (slowSearchTimer.current) { |
| 194 | clearTimeout(slowSearchTimer.current); |
| 195 | } |
| 196 | |
| 197 | const controller = new AbortController(); |
| 198 | searchAbortController.current = controller; |
| 199 | |
| 200 | slowSearchTimer.current = setTimeout(() => { |
| 201 | dispatch({ type: 'SET_LOADING', payload: true }); |
| 202 | }, 200); |
| 203 | |
| 204 | try { |
| 205 | const results = await fileSearch.current.search(state.pattern, { |
| 206 | signal: controller.signal, |
| 207 | maxResults: MAX_SUGGESTIONS_TO_SHOW * 3, |
| 208 | }); |
| 209 | |
| 210 | if (slowSearchTimer.current) { |
| 211 | clearTimeout(slowSearchTimer.current); |
| 212 | } |
| 213 | |
| 214 | if (controller.signal.aborted) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | const suggestions = results.map((p) => ({ |
| 219 | label: p, |
| 220 | value: escapePath(p), |
| 221 | })); |
| 222 | dispatch({ type: 'SEARCH_SUCCESS', payload: suggestions }); |
| 223 | } catch (error) { |
| 224 | if (!(error instanceof Error && error.name === 'AbortError')) { |
| 225 | dispatch({ type: 'ERROR' }); |
| 226 | } |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | if (state.status === AtCompletionStatus.INITIALIZING) { |
| 231 | initialize(); |
no test coverage detected