Skip to content

Commit

Permalink
tsc
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfant committed Nov 16, 2023
1 parent 781a7e5 commit c6e57e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
4 changes: 3 additions & 1 deletion packages/react-urql/src/hooks/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ describe('useQuery', () => {
expect(client.executeQuery).toBeCalledTimes(1);

const [, executeQuery] = result.current;
act(() => executeQuery());
act(() => {
executeQuery();
});
await waitForNextUpdate();
expect(client.executeQuery).toBeCalledTimes(2);
});
Expand Down
46 changes: 26 additions & 20 deletions packages/react-urql/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ export interface UseQueryState<
* };
* ```
*/
export type UseQueryExecute = (opts?: Partial<OperationContext>) => Promise<unknown>;
export type UseQueryExecute = (
opts?: Partial<OperationContext>
) => Promise<unknown>;

/** Result tuple returned by the {@link useQuery} hook.
*
Expand Down Expand Up @@ -355,26 +357,30 @@ export function useQuery<
}, [cache, state[0], state[2][1]]);

const executeQuery = React.useCallback(
(opts?: Partial<OperationContext>) => new Promise((resolve) => {
const context = {
requestPolicy: args.requestPolicy,
...args.context,
...opts,
};
(opts?: Partial<OperationContext>) =>
new Promise<void>(resolve => {
const context = {
requestPolicy: args.requestPolicy,
...args.context,
...opts,
};

deferDispatch(setState, state => {
const source = suspense
? pipe(
client.executeQuery(request, context),
onPush(result => {
cache.set(request.key, result);
})
)
: client.executeQuery(request, context);
source.then(resolve);
return [source, state[1], deps];
});
}),
deferDispatch(setState, state => {
const source = suspense
? pipe(
client.executeQuery(request, context),
onPush(result => {
cache.set(request.key, result);
resolve();
})
)
: pipe(client.executeQuery(request, context), result => {
resolve();
return result;
});
return [source, state[1], deps];
});
}),
[
client,
cache,
Expand Down

0 comments on commit c6e57e4

Please sign in to comment.