Skip to content

Commit

Permalink
chore: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell committed Jan 18, 2024
1 parent d140cdb commit fc27d12
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/react/hooks/useFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ export function useFragment<TData = any, TVars = OperationVariables>(
};
}, [options]);

// TODO: use regular useRef here and set the value inside of useMemo
const resultRef = useLazyRef<UseFragmentResult<TData>>(() =>
diffToResult(cache.diff<TData>(diffOptions))
);
// explain the timing issue: since next is async, we need to make sure that we
// get the correct diff on next render given new diffOptions
React.useMemo(() => {
resultRef.current = diffToResult(cache.diff<TData>(diffOptions));
}, [diffOptions, cache]);

// Used for both getSnapshot and getServerSnapshot
const getSnapshot = React.useCallback(() => resultRef.current, []);
Expand All @@ -78,6 +84,8 @@ export function useFragment<TData = any, TVars = OperationVariables>(
const subscription = cache.watchFragment(options).subscribe({
next: (result) => {
resultRef.current = result;
// TODO: add comment back here
clearTimeout(lastTimeout);
lastTimeout = setTimeout(forceUpdate) as any;
},
});
Expand Down

0 comments on commit fc27d12

Please sign in to comment.