8000 Reactive Map, Set, Date and URL · Issue #10263 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content
Reactive Map, Set, Date and URL #10263
@Rich-Harris

Description

@Rich-Harris

Describe the problem

There's a TODO in the codebase for making reactive maps and sets:

// TODO handle Map and Set as well
if (prototype === object_prototype || prototype === array_prototype) {
const proxy = new Proxy(value, handler);
define_property(value, STATE_SYMBOL, {
value: init(value, proxy 7B56 , immutable),
writable: false
});
// @ts-expect-error not sure how to fix this
return proxy;
}

We could implement it, but there are several problems:

  • we'd need to add the logic for making reactive map/set wrappers in a way that would be impossible to treeshake away for people who weren't using them
  • that cost would increase if we wanted to add support for other built-ins (such as Date, and possibly WeakSet and WeakMap) — including future additions to the language
  • adding support for other built-ins couldn't be done without a breaking change necessitating a major version bump
  • it wouldn't be totally clear which non-POJOs get the reactive treatment
  • there's also no particularly good opt-out mechanism

Describe the proposed solution

Instead, we could just create our own wrappers:

<script>
  import { Map } from 'svelte/reactivity';

  const map = new Map();
  map.set('message', 'hello');

  function update_message() {
    map.set('message', 'goodbye');
  }
</script>

<p>{map.get('message')}</p>

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0