-
-
Notifications
You must be signed in to change notification settings - Fork 35
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: Add Box #207
base: main
Are you sure you want to change the base?
Feat: Add Box #207
Conversation
|
built with Refined Cloudflare Pages Action⚡ Cloudflare Pages Deployment
|
Not too sure I agree with this. Each function will know how often it access the value, and so how important memoization is. Sure, we can flip it around to the caller knowing how often it changes, but is it worth it if the complexity is shifted to everytime the function is called? |
If the name is Derived.by, to mimic Svelte's api, I don't think it should allow a setter, as Svelte's also does not. It is a bit confusing |
Derived and Ref implementing a type called Box is also confusing to me, there's little discoverability. Why not call everything Box? |
I update the PR with the new changes. Dropped the classes. It was indeed a strange API. |
Introduction
This PR solves some of the shortcomings of when it comes to passing state across function boundaries. To preserve reactivity, you have to thunkify the argument
() => T
, but TypeScript doesn't play well with functions when it comes to type inference.The Angular team and Solid's creator Ryan both commented on a TS issue that may solve this issue, but until then, we're stuck with this.
You can use
$derived.by
to turn the getter to a simple property access, but you have to do this manually for every property and for every function call. Lots of boilerplate.Also, if the getter just simply accesses a prop, it doesn't need to be memoized. It's better to move the decision of memoization to the caller rather than the consumer.
box()
The first
Box
implementation isbox()
. It uses$derived
under the hood, so it's memoized.box()
also supports two-way binding by passing a setter as the second argument.ref()
The second
Box
implementation isref()
. It's very similar tobox()
, but doesn't use$derived
. This is more suitable for computations that don't benefit from memoization, like passing props around.ref()
also supports two-way binding by passing a setter as the second argument.Compatibility
Our existing utilities expect you to pass getters and setters around, which is why
Box
exposes theget
andset
properties.