Skip to content

extract onchange callbacks from options #15579

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

Merged
merged 10 commits into from
Mar 22, 2025
Merged

Conversation

Rich-Harris
Copy link
Member

This is a PR against #15069. My thesis is that it's quite unlikely we'll end up with more state options, and until we do we can make things more efficient by just passing the callbacks around rather than creating a bunch of objects and cloning them

cc @paoloricciuti

Copy link

changeset-bot bot commented Mar 21, 2025

⚠️ No Changeset found

Latest commit: 976ff68

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@15579

@paoloricciuti
Copy link
Member

paoloricciuti commented Mar 21, 2025

I like the syntax more but I'm a bit scared because this will lock us into not adding any new option before svelte 6 unless we go the

$state(0, ()=>{}, {...});

Or with variadic arguments.

That said I agree is probably unlikely we would need to add any new option (I can't think of anything) and we could always resort to a new rune.

@paoloricciuti
Copy link
Member

Oh wait I'm super wrong and I completely misunderstood the PR 😑

I shouldn't check those on the phone lol

@Rich-Harris
Copy link
Member Author

Huh, I'm amazed we currently allow spread arguments to $state. I think we should just disallow that

@paoloricciuti
Copy link
Member

But yeah I think this basically forces the argument to be statically analyzable to pass the function which we decided against no?

@Rich-Harris
Copy link
Member Author

I think there's a meaningful distinction between allowing spread elements (which no-one would ever need) and allowing arbitrary expressions for the second argument (which is potentially useful)

@paoloricciuti
Copy link
Member

Yeah I think we can just disallow spread

@paoloricciuti
Copy link
Member

Would that be considered a breaking tho? I don't think no one in their right mind ever did this but... 😁

@Ocean-OS
Copy link
Contributor

I think that should be a breaking change just in case.

@paoloricciuti
Copy link
Member

paoloricciuti commented Mar 21, 2025

The alternative is to unwrap the rest

$.state(args[0], args[1].onchange);

@Rich-Harris
Copy link
Member Author

I mean we could do that but if someone has actually written that code then god help them. I don't think this rises to the level of 'breaking change'

@paoloricciuti
Copy link
Member

I mean we could do that but if someone has actually written that code then god help them. I don't think this rises to the level of 'breaking change'

I have the change locally...should i push it?

@paoloricciuti
Copy link
Member

i pushed it...we can always revert if we don't want it

@Rich-Harris
Copy link
Member Author

I think it adds a lot of needless complexity, I'd much rather we just prohibit spread arguments with runes (except perhaps $inspect(...stuff)

@paoloricciuti
Copy link
Member

I think it adds a lot of needless complexity, I'd much rather we just prohibit spread arguments with runes (except perhaps $inspect(...stuff)

I mean...i think we are safe 😄😄😄

@paoloricciuti
Copy link
Member

Opened a PR to prevent spreads

#15585

@adiguba
Copy link
Contributor

adiguba commented Mar 22, 2025

Hi,

My two cents : I think that the compiler should throw an error when options contains incorrect properties :

let name = $state('world', {
	xxx: 42 // should throw "Invalid option for $state()"
});

@paoloricciuti
Copy link
Member

Hi,

My two cents : I think that the compiler should throw an error when options contains incorrect properties :

let name = $state('world', {
	xxx: 42 // should throw "Invalid option for $state()"
});

We removed a bunch of this compiler errors deferring the work to typescript

@Rich-Harris
Copy link
Member Author

Since we're now disallowing spreads I'll go ahead and merge this

@Rich-Harris Rich-Harris merged commit 714c042 into state-onchange Mar 22, 2025
10 checks passed
@Rich-Harris Rich-Harris deleted the state-onchange-tweak branch March 22, 2025 13:14
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.

4 participants