Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
Replace ref with setState, batch updates
Browse files Browse the repository at this point in the history
  • Loading branch information
msutkowski committed Mar 22, 2021
1 parent 53ed749 commit 25d1a65
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions src/react-hooks/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type UseLazyQuery<D extends QueryDefinition<any, any, any, any>> = <R = U

export type UseLazyQuerySubscription<D extends QueryDefinition<any, any, any, any>> = (
options?: SubscriptionOptions
) => [(args: QueryArgFrom<D>) => void, undefined | React.MutableRefObject<QueryActionCreatorResult<any>>['current']];
) => [(args: QueryArgFrom<D>) => void, undefined | QueryActionCreatorResult<any>];

export type QueryStateSelector<R, D extends QueryDefinition<any, any, any, any>> = (
state: QueryResultSelectorResult<D>,
Expand Down Expand Up @@ -252,57 +252,57 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
refetchOnFocus,
pollingInterval = 0,
} = {}) => {
const forceUpdate = useReducer(() => ({}), {})[1];
const { initiate } = api.endpoints[name] as ApiEndpointQuery<
QueryDefinition<any, any, any, any, any>,
Definitions
>;
const dispatch = useDispatch<ThunkDispatch<any, any, AnyAction>>();

const promiseRef = useRef<QueryActionCreatorResult<any>>();
const lastPromise = promiseRef.current;
const [promiseRef, setPromiseRef] = useState<QueryActionCreatorResult<any>>();

useEffect(() => {
const subscriptionOptions = {
refetchOnReconnect,
refetchOnFocus,
pollingInterval,
};
if (lastPromise && !shallowEqual(subscriptionOptions, lastPromise.subscriptionOptions)) {
lastPromise?.updateSubscriptionOptions(subscriptionOptions);
lastPromise.subscriptionOptions = subscriptionOptions;
if (promiseRef && !shallowEqual(subscriptionOptions, promiseRef.subscriptionOptions)) {
promiseRef?.updateSubscriptionOptions(subscriptionOptions);
setPromiseRef((prev) => ({ ...prev!, subscriptionOptions }));
}
}, [lastPromise, refetchOnFocus, refetchOnReconnect, pollingInterval]);
}, [promiseRef, refetchOnFocus, refetchOnReconnect, pollingInterval]);

useEffect(() => {
return () => {
promiseRef.current?.unsubscribe();
promiseRef.current = undefined;
promiseRef?.unsubscribe();
};
}, []);
}, [promiseRef]);

const trigger = useCallback(
function (arg: any) {
lastPromise?.unsubscribe();

const subscriptionOptions = {
refetchOnReconnect,
refetchOnFocus,
pollingInterval,
};

promiseRef.current = dispatch(
initiate(arg, {
subscriptionOptions,
forceRefetch: true,
})
);
forceUpdate();
batch(() => {
promiseRef?.unsubscribe();

const subscriptionOptions = {
refetchOnReconnect,
refetchOnFocus,
pollingInterval,
};

const promise = dispatch(
initiate(arg, {
subscriptionOptions,
forceRefetch: true,
})
);

setPromiseRef(promise);
});
},
[dispatch, initiate, lastPromise, pollingInterval, refetchOnFocus, refetchOnReconnect]
[dispatch, initiate, promiseRef, pollingInterval, refetchOnFocus, refetchOnReconnect]
);

return useMemo(() => [trigger, lastPromise], [trigger, lastPromise]);
return useMemo(() => [trigger, promiseRef], [trigger, promiseRef]);
};

const useQueryState: UseQueryState<any> = (
Expand Down

0 comments on commit 25d1a65

Please sign in to comment.