-
Notifications
You must be signed in to change notification settings - Fork 316
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
Move Project Manager into cached React Query to ensure it is globally unique #9648
base: develop
Are you sure you want to change the base?
Conversation
f1429e9
to
3b9f824
Compare
const [error, setError] = React.useState<unknown>(null) | ||
const isLoading = supportsLocalBackend && rootDirectoryPath == null | ||
const [projectManager, setProjectManager] = React.useState<ProjectManager | null>(null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think with react-query we don't need to write code like that anymore. All we need is to:
// it's mostly a pseudocode
export function useProjectManager(){
return useQuery({
queryKey: ['projectManager'],
queryFn: ...
})
}
Please refer to the documentation for additional info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TLDR:
- Create custom hook 'useProjectManager()`
- configure
useQuery
to store the current project manager as long as needed (maybe forever?) By default, it stores data as long as it's in use and 5 minutes after unmount of the latest component that uses this data. - Use that hook in the app.
- Additionally, we might want to use
useSuspenceQuery
to integrate fetching withReact.Suspense
- Also, there's a possibility to prefetch data with
usePrefetchQuery
, so it might be helpful to separate query options fromuseQuery
, You can usequeryOptions
function for that: https://tanstack.com/query/latest/docs/framework/react/typescript#typing-query-options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true, but as this is a fix i think it's better to avoid migrating to react-query for now, if we really should migrate then we can do that in another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, because migrating to react query makes this PR obsolete(it'll be not the next step, but a whole rewrite, and it's relatively easy to rewrite).
If you need any help with rewriting to react-query
please reach me in the DM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while that's true, the part i'm unsure about isn't so much about using react query...
... it's about (ab)using react query as a form of permanent global state. i think it'd make it harder for users to figure out how the project manager is being cached, especially if they are unfamiliar with React Query.
iow: we have all other globals in Contexts, i'd prefer staying consistent and maybe switch to React Query after figuring out what we should and shouldn't be keeping in React Query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's okay to use React.Context to provide variables that don't require fetching or accessing asynchronously, such as clipboard or geolocation. For asynchronous tasks, I strongly recommend using react-query. There's a clear distinction between React.Context and react-query. As far as I can see, we need to fetch some data before we create a ProjectManager instance, which is why I suggest rewriting it using react-query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, but again the issue is not with using react-query, but rather with using react-query as hidden global state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more about proper working with the async flow, integration with React.Suspense
, prefetching, invalidation, avoiding bad practices(ignoring errors, fetching in useEffect), consistency across the codebase(we're moving away from fetching in useEffect) and so on.
The global state goes latter
Pull Request Description
React.useEffect
firing twice when Strict Mode is enabled.npm run dev
inapp/gui2
when there are broken symlinks in the projects directoryImportant Notes
None
Testing Instructions
127.0.0.1:30535
to make sure that only one of them has any messages at allChecklist
Please ensure that the following checklist has been satisfied before submitting the PR:
The documentation has been updated, if necessary.Screenshots/screencasts have been attached, if there are any visual changes. For interactive or animated visual changes, a screencast is preferred.Scala,
Java,
and
Rust
style guides. In case you are using a language not listed above, follow the Rust style guide.
Unit tests have been written where possible.If GUI codebase was changed, the GUI was tested when built using./run ide build
.