Skip to content

Commit

Permalink
Bind observableQuery subscribeToMore and remove need for useCallback …
Browse files Browse the repository at this point in the history
…in suspense hooks
  • Loading branch information
jerelmiller committed Jul 5, 2024
1 parent 92dcc18 commit 141a0e1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export class ObservableQuery<
this.waitForOwnResult = skipCacheDataFor(options.fetchPolicy);
this.isTornDown = false;

this.subscribeToMore = this.subscribeToMore.bind(this);

const {
watchQuery: { fetchPolicy: defaultFetchPolicy = "cache-first" } = {},
} = queryManager.defaultOptions;
Expand Down
12 changes: 5 additions & 7 deletions src/react/hooks/useBackgroundQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,14 @@ function _useBackgroundQuery<
[queryRef]
);

const subscribeToMore: SubscribeToMoreFunction<TData, TVariables> =
React.useCallback(
(options) => queryRef.observable.subscribeToMore(options),
[queryRef]
);

React.useEffect(() => queryRef.softRetain(), [queryRef]);

return [
didFetchResult.current ? wrappedQueryRef : void 0,
{ fetchMore, refetch, subscribeToMore },
{
fetchMore,
refetch,
subscribeToMore: queryRef.observable.subscribeToMore,
},
];
}
12 changes: 5 additions & 7 deletions src/react/hooks/useQueryRefHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,9 @@ function _useQueryRefHandlers<
[internalQueryRef]
);

const subscribeToMore: SubscribeToMoreFunction<TData, TVariables> =
React.useCallback(
(options) => internalQueryRef.observable.subscribeToMore(options),
[internalQueryRef]
);

return { refetch, fetchMore, subscribeToMore };
return {
refetch,
fetchMore,
subscribeToMore: internalQueryRef.observable.subscribeToMore,
};
}
8 changes: 1 addition & 7 deletions src/react/hooks/useSuspenseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,7 @@ function _useSuspenseQuery<
[queryRef]
);

const subscribeToMore: SubscribeToMoreFunction<
TData | undefined,
TVariables
> = React.useCallback(
(options) => queryRef.observable.subscribeToMore(options),
[queryRef]
);
const subscribeToMore = queryRef.observable.subscribeToMore;

return React.useMemo<
UseSuspenseQueryResult<TData | undefined, TVariables>
Expand Down

0 comments on commit 141a0e1

Please sign in to comment.