Skip to content

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Feb 28, 2024

Fixes #10643.

Copy link

changeset-bot bot commented Feb 28, 2024

🦋 Changeset detected

Latest commit: d02511d

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

@brunnerh
Copy link
Member

This looks like it removes memoization, which could still be relevant if the value is used in multiple places and is expensive to compute.

@trueadm
Copy link
Contributor Author

trueadm commented Feb 28, 2024

This looks like it removes memoization, which could still be relevant if the value is used in multiple places and is expensive to compute.

Can you provide an example?

@brunnerh
Copy link
Member

Before, the value was set in a field once, hence it only got computed once.
Now it gets computed on every get.

	class Counter {
		count = 0;
-		doubled = this.count * 2;
+		get doubled() {
+			return this.count * 2;
+		}

		constructor(initialCount = 0) {
			this.count = initialCount;
		}
	}

@trueadm
Copy link
Contributor Author

trueadm commented Feb 28, 2024

@brunnerh I think this should be fine in all likelihood. It wasn't that the value was being memoized before, it was just always static and never changed. So if you changed this.state, then doubled would never get updated – which was a major bug. If we were to bring in memoization again then we'd need invalidation for when it's changed, which means adding a lot more logic into the SSR runtime.

@brunnerh
Copy link
Member

brunnerh commented Feb 28, 2024

A static value is a form of memoization.

Couldn't the assignment of all derived fields just be moved to the end of the constructor?

(Also, I would not even bother using $derived if it weren't for its memoization, so that being gone in SSR is not good.)

@trueadm
Copy link
Contributor Author

trueadm commented Feb 28, 2024

@brunnerh Nice call. #10661

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.

$derived class fields get incorrect value on the server when $state is updated in the constructor

2 participants