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

feat: use __SWR_DEVTOOLS_USE__ hook #55

Merged
merged 10 commits into from
Jul 1, 2022
Merged

Conversation

koba04
Copy link
Owner

@koba04 koba04 commented Jun 3, 2022

fixes #52

This requires to add the hook into swr, so I'm working on it in https://github.com/koba04/swr/tree/devtools-hook

@vercel
Copy link

vercel bot commented Jun 3, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
swr-devtools ✅ Ready (Inspect) Visit Preview Jun 28, 2022 at 3:40PM (UTC)

@koba04
Copy link
Owner Author

koba04 commented Jun 3, 2022

If we inject a middleware from a web accessible resource, we have to use the same the React instance the application is using, otherwise we have an invalid hook call error. That seems to be a bit tricky.

@koba04
Copy link
Owner Author

koba04 commented Jun 3, 2022

13506f8 just sends post messages in a middleware. This works fine, but it only gives us limited functionality.

@koba04
Copy link
Owner Author

koba04 commented Jun 6, 2022

f61c092 uses the existing middleware without the use of React Hooks.

@koba04
Copy link
Owner Author

koba04 commented Jun 6, 2022

In this approach, we don't have a way to invoke discarded events because we cannot use a cleanup function of useEffect.
One possible way to fix this is to expose the React instance through a global variables like window.__SWR_DEVTOOLS_REACT.

@koba04 koba04 changed the title [WIP] feat: use __SWR_DEVTOOLS_USE__ hook feat: use __SWR_DEVTOOLS_USE__ hook Jun 28, 2022
@koba04 koba04 marked this pull request as ready for review June 28, 2022 15:19
@shuding
Copy link
Collaborator

shuding commented Jun 28, 2022

I'll do a beta release of SWR these two days and we can try it out. ❤️

const { useLayoutEffect, useEffect, useRef } =
typeof window !== "undefined" &&
// @ts-expect-error
typeof window.__SWR_DEVTOOLS_REACT__ !== "undefined"
Copy link

@huozhi huozhi Jun 29, 2022

Choose a reason for hiding this comment

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

@koba04 Do you feel we could also use a "hook" or "callback" thing to pass down more stuff? like

const { React: SharedReact } = window.__SWR_DEVTOOLS_HOOK__()
const { useLayoutEffect, useEffect, useRef } = SharedReact

once we need anything else besides the shared React, we can pass more parameters to the return value of the callback. so that the devtools protocol might not need to change in the future if the return values is changed. wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you have any example for things other than React that we might need @huozhi?

Copy link

Choose a reason for hiding this comment

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

Not yet, I just feel it's easier to not break the protocol, for now we only need React, but later we might have others. Returning from a hook function can be pretty flexible

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think of React as a special case here (because the same instance in required) and we don’t want to pass around things in general.

Copy link
Owner Author

Choose a reason for hiding this comment

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

@huozhi @shuding
As @shuding mentioned, React is a special case for that reason, so we don't have other things we'd like to pass.
But something might come up in the future. In this case, I can add a new "hook" like __SWR_DEVTOOLS_HOOK__ and include React in the hook, so I feel __SWR_DEVTOOLS_REACT__ is enough at this time.

Copy link

@huozhi huozhi Jun 30, 2022

Choose a reason for hiding this comment

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

Yeah that sounds fair. Maybe a renaming to SWR_DEVTOOLS_XXX else not tight to react itself can be nicer since it's not that too specific, and could be upgraded loosely without needed to be tight with swr version. But it's just my personal thought not a blocking at all for any current change 👍 🚀

If devtool is tighted with version then we can also change it later when it's necessary

@koba04
Copy link
Owner Author

koba04 commented Jul 1, 2022

vercel/swr#2016 has been merged, so I'll merge this too. If there is any issue, I'll fix it later.

@koba04 koba04 merged commit 7f022b6 into main Jul 1, 2022
@koba04 koba04 deleted the feat-swr-devtools-use-hook branch July 1, 2022 13:35
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.

Injecting the middleware automatically
3 participants