Code Monkey home page Code Monkey logo

Comments (8)

trueadm avatar trueadm commented on June 11, 2024 3

You can use a class to handle external state and trigger invalidate on it to do as you want:

class External {
    #data;
    #version = $state(0);

    constructor(data) {
        this.#data = data;
    }
    get data() {
        this.#version;
        return this.#data;
    }
    set data(_data) {
        this.#version++;
        this.#data = _data;
    }
    invalidate() {
        this.#version++;
    }
}

from svelte.

TheCymaera avatar TheCymaera commented on June 11, 2024 1

$state.frozen doesn't seem to trigger an update either. In fact, it freezes the object, preventing the external API from mutating it.
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAEyWOwYqDQBBEf2VocnBAsu7VjYLHfMN2DqP2ZJVJ9zDTClnx34PJsV69gtrAT4Ey1L8bsHsQ1NDFCCXoMx4hrxSUoIQsSxoOcslDmqKa4PjeIGhGaJEDqZF-No05ZXVKZ5_kn7jYdvuDjJxJr6yUVheKwjbtx5V-Ls13VVX2cE7kPQ367jdkVOnnY6yDcJZA5yD3AqFjs8TRKZk_FyMxjQgWebfIl6_PuRZKeMg4-YlGqDUttN_2F9f91XzrAAAA

from svelte.

paoloricciuti avatar paoloricciuti commented on June 11, 2024 1

Try to use $effect sparsely: it should be used solely to sync a reactive variable with some imperative API. Abusing it to trigger a re fetch doesn't seem like a good idea where you could just write a function that fetch and assign and call that in the code instead of reassigning.

from svelte.

luisfontes avatar luisfontes commented on June 11, 2024

Isn't this what $state.frozen is for?

from svelte.

brunnerh avatar brunnerh commented on June 11, 2024

You can nest the data in a simple wrapper, then create a new wrapper in the assignment.
The actual data itself will not need to be copied.

let obj = $state({ data: {} });
setInterval(() => obj = { ...obj }, 1000)

from svelte.

7nik avatar 7nik commented on June 11, 2024

What are your real cases? Why do you want to do something if the data didn't change?

from svelte.

mjadobson avatar mjadobson commented on June 11, 2024

A quick and dirty fix to get the v4 behaviour (without object cloning) is to set the variable to undefined, and then reset it back to the previous state:

<script lang="ts">
	let obj = $state({});
	
	setInterval(()=> {
		let tmp = obj;
		obj = undefined;
		obj = tmp;
	}, 1000)
	
	$effect(()=>{
		obj;
		console.log("An update happened")
	})
</script>

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE02PwWrDMBBEf0UsOVhgWvfqxIYc8w3dHFRrlarIK2GtA0Ho34uSHnqc4b2BKeB8oAzjZwE2K8EI55SgB3mkFvKdghD0kOO-La055WXzSVQwfJsQJCPMyIFExa8fNalDFiPUlaqPyMiZ5MJC292ErtPTrAoySsNlTWpqUuPkJe9syXkm-6-TNR2Ra68-hmHQbfNAztEiz73yBz6FJXKOgd5CvHUIZ1Z7skZIfZuUiMkiaOSqkU_vrxcz9LBG650nC6NsO9Vr_QUdi1XSFAEAAA==

from svelte.

TheCymaera avatar TheCymaera commented on June 11, 2024

I just had a look at the reactive collections provided by Svelte (in svelte/reactivity). It works by using an incrementing counter similar to what trueadm suggested.

I'm glad to find some workarounds, but it would be nice to have a more idiomatic solution down the line. I'm willing to contribute a PR if the members can agree on a solution.

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.