MCPcopy
hub / github.com/adobe/react-spectrum / dispatchFetch

Function dispatchFetch

packages/react-stately/src/data/useAsyncList.ts:300–327  ·  view source on GitHub ↗
(action: Action<T, C>, fn: AsyncListLoadFunction<T, C>)

Source from the content-addressed store, hash-verified

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(() => {

Callers 5

useAsyncListFunction · 0.85
reloadFunction · 0.85
loadMoreFunction · 0.85
sortFunction · 0.85
setFilterTextFunction · 0.85

Calls 2

fnFunction · 0.85
sliceMethod · 0.80

Tested by

no test coverage detected