Skip to content

Svelte 5: decide on behaviour of $state.frozen #12434

Closed
@Rich-Harris

Description

@Rich-Harris

Describe the problem

The discussion in #12413 revealed some differences of opinion around $state.frozen. As of that PR, it will error if passed existing reactive state, which eliminates one source of confusion. But others remain:

  • in dev, the input will be frozen with Object.freeze, but in prod it won't (for perf reasons). As such, the name is a bit of a misnomer
  • Object.freeze is so-called for a reason — it freezes the object passed in; it has a side-effect. frozen implies that it returns a frozen object, rather than freezing its argument as a side-effect. Which isn't to say that $state.freeze would be a better name, since that implies that it freezes existing state proxies
  • It's shallow — it doesn't affect state inside the object, including in classes

Describe the proposed solution

Together, these observations suggest (to me at least) we should change the name and the behaviour. Taking a cue from MobX, perhaps $state.ref would be a better name. $state.raw is another option. Doubtless there are others.

This would have similar semantics to $state.frozen today — in other words only reassigning (not mutating) will cause updates (or in implementation terms: it creates a source but not a proxy) — but without the dev-time Object.freeze.

Of course, by doing this we lose the opportunity to tell people that they're incorrectly mutating an object. If we want to keep that but also have consistency between dev and prod, then the alternative is to always use Object.freeze, perf implications be damned.

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions