-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
@wagmi/svelte #4365
base: main
Are you sure you want to change the base?
@wagmi/svelte #4365
Conversation
Biome currently only has partial support for Svelte files. It can successfully format the <script> portion, but when linting it doesn't check the HTML part, leading to many variables being marked as unused.
This allows us to accept a function as a parameter, which allows for reactivity (if we pass a raw value, it won't be reactive).
I'm not sure how to get Svelte tests into the Vitest workspace. Without installing a bunch of dependencies into the root folder, it cannot run the Svelte-specific code.
This required a bit more refactoring on the testing system. There are still a few things I haven't figured out yet, but we're getting there :).
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎ To accept the risk, merge this PR and you will not be notified again.
Next stepsWhat is obfuscated code?Obfuscated files are intentionally packed to hide their behavior. This could be a sign of malware. Packages should not obfuscate their code. Consider not using packages with obfuscated code Take a deeper look at the dependencyTake a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev. Remove the packageIf you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency. Mark a package as acceptable riskTo ignore an alert, reply with a comment starting with
|
Not only was this a bad choice for users (as they might not know why it's not being reactive), it also turns out to be harder to use in types.
Svelte only allows us to call getContext() when a component initializes.
This draft PR is a WIP implementation of reactive Wagmi hooks in Svelte.
Notes
Reactivity
In order to make return types reactive, I have opted to return a function. It's the function that then returns the value. While it is a little unintuitive, it works perfectly with
$derived.by
:Design consideration: should we use React-style
.current
instead of returning a function?Why?
Unfortunately, simply returning a primitive from a function doesn't make it reactive. The workaround is to return an object with a getter:
However, I find this a little ugly when used, especially because it cannot be destructured:
Similarly, a similar problem occurs with parameters: if a reactive object is directly passed into a function, it won't be reactive:
Happily, this isn't the only framework that has this problem (SolidJS shares a similar problem). In both frameworks, the workaround is to simply pass in a function as a parameter:
Design consideration: In theRuneParameters
type, I have defined it as(() => T) | T
. In other words, it accepts either a reactive function or a non-reactive object. Will this lead to confusion?Edit: Now we can only pass in a function. This makes it easier to compose types and reduces confusion for users.
Design consideration: should the return types and parameters include the function or not?
ReturnType<UseChainIdReturnType>
orReturnType<UseChainIdParameters>
)Testing
So far, I don't see a way of testing the Svelte package without adding the dependencies to the root. For now, I'm simply running
vitest
in the package instead of adding it to the Vitest workspace -- I will look into it in the future.