Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify use of pause #278

Merged
merged 1 commit into from
Jun 8, 2019
Merged

Modify use of pause #278

merged 1 commit into from
Jun 8, 2019

Conversation

andyrichardson
Copy link
Contributor

@andyrichardson andyrichardson commented Jun 8, 2019

The proposed changes here are aimed to simplify the use-cases associated with the pause argument.

Rather than pause preventing requests outright - it will pause the functionality of auto fetching - whether that be on mount or when the query has changed.

The reasons for this are:

  • It allows users to pause auto fetching while having the ability to manually fetch on demand (currently not an option - see example problem here)
  • It improves the seperation of concerns. Config options should influence how urql acts, manual operations can be prevented with a conditional inside component logic.

There's also a small typing fix for useImmediateEffect.

Example usage
const MySearch = () => {
  const [variables, setVariables] = useState({ searchString: "" });
  const [searchResults, fetchSearchResults] = useQuery({ query, variables, skip: true });

  const throttledFetch = throttle(fetchSearchResults, 200); 

  const handleChange = (e) => {
    setVariables({ searchString: e.target.value });
    throttledFetch();
  };

  return (
    <input value={vars.searchString} onChange={handleChange} />
    <Results values={searchResults} />
  )
}

Additional notes

The advantages of this approach are pretty clear but it does highlight a bug that we haven't yet addressed - updating variables/query values and immediately executing them in the same render doesnt act as expected.

I've created an issue about this and I believe the best way to go about this is to allow arguments to be overridden when manually fetching.

@andyrichardson andyrichardson changed the title Simplify use of pause Modify use of pause Jun 8, 2019
);

// This calls executeQuery immediately during the initial mount and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good but it'd be great to readd the comments and slightly rewrite them. Especially the one here that explains with this is using useImmediateEffect 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup agreed - I'll add something back in 👍

@kitten kitten merged commit 36c5d59 into master Jun 8, 2019
@kitten kitten deleted the useQuery-changes branch June 8, 2019 13:49
changes: ReadonlyArray<any>
) => {
const teardown = useRef(noop);
const teardown = useRef<ReturnType<EffectCallback>>(undefined);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we initializing this to undefined with the expectation that teardown.current will never get called until it gets reassigned to the return type of effect? Seems like a safe assumption based on how this effect runs (teardown synchronously getting re-assigned in the first if below) but just wanted to get the reasoning on this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, should be safe since teardown functions can be undefined in the worst case 👍

useImmediateEffect(() => {
if (args.pause) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice I like this. So the flow here is:

  • We check args.pause on initial mount to handle the case where a user just wants the executeQuery fn.
  • Also check it on all subsequent changes and prevent queries when things update.

❤️Love it. My one question is should L69, if it's being treated as the cleanup function, be written as () => setState(s => ({ ...s, fetching: false }));? Seems we're returning void currently rather than () => void.

Copy link
Contributor

@parkerziegler parkerziegler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small questions but this looks awesome @andyrichardson and I can see how it solves the use case you mention ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants