(action: Action<T, C>, fn: AsyncListLoadFunction<T, C>)
| 298 | }); |
| 299 | |
| 300 | const dispatchFetch = async (action: Action<T, C>, fn: AsyncListLoadFunction<T, C>) => { |
| 301 | let abortController = new AbortController(); |
| 302 | try { |
| 303 | dispatch({...action, abortController}); |
| 304 | let previousFilterText = action.filterText ?? data.filterText; |
| 305 | |
| 306 | let response = await fn({ |
| 307 | items: data.items.slice(), |
| 308 | selectedKeys: data.selectedKeys, |
| 309 | sortDescriptor: action.sortDescriptor ?? data.sortDescriptor, |
| 310 | signal: abortController.signal, |
| 311 | cursor: action.type === 'loadingMore' ? data.cursor : undefined, |
| 312 | filterText: previousFilterText, |
| 313 | loadingState: data.state |
| 314 | }); |
| 315 | |
| 316 | let filterText = response.filterText ?? previousFilterText; |
| 317 | dispatch({type: 'success', ...response, abortController}); |
| 318 | |
| 319 | // Fetch a new filtered list if filterText is updated via `load` response func rather than list.setFilterText |
| 320 | // Only do this if not aborted (e.g. user triggers another filter action before load completes) |
| 321 | if (filterText && filterText !== previousFilterText && !abortController.signal.aborted) { |
| 322 | dispatchFetch({type: 'filtering', filterText}, load); |
| 323 | } |
| 324 | } catch (e) { |
| 325 | dispatch({type: 'error', error: e as Error, abortController}); |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | let didDispatchInitialFetch = useRef(false); |
| 330 | useEffect(() => { |
no test coverage detected