Skip to content

fix: make $state component exports settable #12345

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 3 commits into from
Jul 8, 2024
Merged

Conversation

dummdidumm
Copy link
Member

fixes #11983

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jul 8, 2024

🦋 Changeset detected

Latest commit: 71cd00a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

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

@FoHoOV
Copy link
Contributor

FoHoOV commented Jul 8, 2024

This works as the tests say, but if we just try to use the "assign directly" method by removing the increment function it doesnt work. It seems if we never try to mutate the state within that component, then it just changes into a normal variable.

with increment:

export default function Counter($$anchor, $$props) {
	$.push($$props, true);

	let count = $.source(0);
        // other stuff
	return $.pop({
		get count() {
			return $.get(count);
		},
		set count($$value) {
			$.set(count, $.proxy($$value));
		},
		get doubled() {
			return $.get(doubled);
		},
		increment
	});

without increment:

export default function Counter($$anchor, $$props) {
	$.push($$props, true);

	let count = 0;
        // other stuff
	return $.pop({
		get count() {
			return count;
		},
		set count($$value) {
			count = $$value;
		},
		get doubled() {
			return $.get(doubled);
		}
	});

Edit: this behavior is not related to this PR, but seems to break what people or at least I would expect it to do.

Edit2: because of this maybe?

export function is_state_source(binding, state) {
	return (
		(binding.kind === 'state' || binding.kind === 'frozen_state') &&
		(!state.analysis.immutable || binding.reassigned || state.analysis.accessors)
	);
}

@Rich-Harris
Copy link
Member

good spot — fixed

@Rich-Harris Rich-Harris merged commit e8c3729 into main Jul 8, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the settable-state-export branch July 8, 2024 16:27
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.

Add setter to let variables accessors
3 participants