Skip to content

Commit

Permalink
refactor: 💡 adjusted keys comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
prc5 committed Dec 3, 2024
1 parent 725c037 commit 1a1b5d7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ export const useRequestEvents = <T extends RequestInstance>({
// Lifecycle
// ******************

const handleGetLoadingEvent = (queueKey: string) => {
const handleGetLoadingEvent = (req: T) => {
return ({ loading }: RequestLoadingEventType) => {
const isProcessing = getIsDataProcessing();
const isProcessing = getIsDataProcessing(req.cacheKey);

// When we process the cache data, we don't want to change the loading state during it
// This prevents the UI from flickering with { data: null, loading: false }
if (isProcessing) return;

const canDisableLoading = !loading && !dispatcher.hasRunningRequests(queueKey);
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(req.queueKey);
if (loading || canDisableLoading) {
actions.setLoading(loading, false);
}
Expand Down Expand Up @@ -174,7 +174,7 @@ export const useRequestEvents = <T extends RequestInstance>({

const addCacheDataListener = (req: T) => {
// Data handlers
const loadingUnmount = requestManager.events.onLoading(req.queueKey, handleGetLoadingEvent(req.queueKey));
const loadingUnmount = requestManager.events.onLoading(req.queueKey, handleGetLoadingEvent(req));
const getResponseUnmount = cache.events.onData<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>(
req.cacheKey,
setCacheData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type UseRequestEventsPropsType<T extends RequestInstance> = {
logger: LoggerType;
actions: UseTrackedStateActions<T>;
setCacheData: (cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>) => void;
getIsDataProcessing: () => boolean;
getIsDataProcessing: (cacheKey: string) => boolean;
};

export type UseRequestEventsActionsType<T extends RequestInstance> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ export const useTrackedState = <T extends RequestInstance>({
}
};

const getIsDataProcessing = () => {
return isProcessingData.current === cacheKey;
const getIsDataProcessing = (processingCacheKey: string) => {
return isProcessingData.current === processingCacheKey;
};

const setCacheData = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type UseTrackedStateReturn<T extends RequestInstance> = [
setRenderKey: (renderKey: keyof UseTrackedStateType<T>) => void;
setCacheData: (cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>) => void;
getStaleStatus: () => boolean;
getIsDataProcessing: () => boolean;
getIsDataProcessing: (cacheKey: string) => boolean;
},
];

Expand Down

0 comments on commit 1a1b5d7

Please sign in to comment.