Code Monkey home page Code Monkey logo

Comments (4)

paoloricciuti avatar paoloricciuti commented on August 20, 2024 1

The problem here is that for back compatibility actions are treated as coarse. When you pass an object it deeply reads it to retrigger everytime something changes.

// Action's update method is coarse-grained, i.e. when anything in the passed value changes, update.
// This works in legacy mode because of mutable_source being updated as a whole, but when using $state
// together with actions and mutation, it wouldn't notice the change without a deep read.
deep_read_state(value);

however deep_read_state can't work with classes since getters and setters are not enumerable.

In theory we could check and access every getter on the prototype...i'll see if there's a decent way of doing it.

from svelte.

paoloricciuti avatar paoloricciuti commented on August 20, 2024 1

Ugh i tried to do something like this

const is_object_literal =
	typeof value === 'object' && value !== null && value.constructor === Object;

if (STATE_SYMBOL in value) {
	deep_read(value);
} else if (!Array.isArray(value)) {
	if (is_object_literal && Object.keys(value).length === 0) {
		for (let key in value) {
			const prop = value[key];
			if (typeof prop === 'object' && prop && STATE_SYMBOL in prop) {
				deep_read(prop);
			}
		}
	} else {
		const keys = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(value));
		for (const key in keys) {
			if (keys[key].get) {
				value[key];
			}
		}
	}
}

but there's Proxy involved so is_object_literal gives false positives and it messes with the legacy labeled statement.

Another option could be dynamically add a symbol property to every compiled class that enumerates the fields but it's a pretty involved modification so i would love to hear the opinion of the maintainers before going on 😄

from svelte.

brunnerh avatar brunnerh commented on August 20, 2024

I don't think it should trigger in either case with fine-grained reactivity 🤔

With runes I would probably use an effect instead of the update function:

const myAction = (node, counter) => {
	$effect(() => {
		node.innerHTML = counter.value;
	});
}

REPL

from svelte.

dummdidumm avatar dummdidumm commented on August 20, 2024

We shouldn't change anything here. The rule is "POJOs are auto-fine-grained and classes are not", and we shouldn't make an exception for actions. If using actions + runes one shouldn't use the update return type but instead use $effect to react to changes.

from svelte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.