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

bugfix: avoid returning stale data in useDocument and useHandle #347

Merged
merged 9 commits into from
May 7, 2024

Conversation

geoffreylitt
Copy link
Collaborator

useDocument and useHandle had a footgun where after the input url changed, they would continue returning the old doc/handle for the previous input for 1 or more renders.

This behavior could cause problems in downstream computations or useEffect hooks which did not account for this brief inconsistency.

This patch fixes the problem by checking whether the input and output match before returning out of the hook. In useHandle we can just check the handle URL directly; in useDocument we need to store some additional state to remember the URL associated with a given doc.

tests

useDocument and useHandle had a footgun where after the input url changed,
they would continue returning the old doc/handle for the previous input
for 1 or more renders.

This behavior could cause problems in downstream computations or
useEffect hooks which did not account for this brief inconsistency.

This patch fixes the problem by checking whether the input and output
match before returning out of the hook. In useHandle we can just
check the handle URL directly; in useDocument we need to store some
additional state to remember the URL associated with a given doc.
@@ -31,6 +31,7 @@ export {
isValidAutomergeUrl,
parseAutomergeUrl,
stringifyAutomergeUrl,
interpretAsDocumentId,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

any reason not to export this? felt useful

Copy link
Member

Choose a reason for hiding this comment

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

nope, it's fine

@pvh
Copy link
Member

pvh commented May 6, 2024

I took a stab at simplifying this patch a bit @geoffreylitt -- useHandle it turns out could be mostly deleted and I wanted to at least take a stab at simplifying useDocument. This is where I wound up. On the whole I'm increasingly convinced that we should be using some kind of state management library instead of this but hey, here we are.

Tests pass, but note that enabling or disabling the setDoc(v) on line 53 has no effect on the success of the tests.

@geoffreylitt
Copy link
Collaborator Author

@pvh I simplified the code further based on your idea, and I also improved the tests to ensure that URL changes lead to immediately updated data on first render.

@pvh pvh merged commit e8298f9 into main May 7, 2024
2 checks passed
@pvh
Copy link
Member

pvh commented May 7, 2024

Merged & shipped in 1.1.10.

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.

2 participants