fix: ensure SvelteMap reactivity persists through deriveds (#13877)

* fix: ensure SvelteMap reactivity persists through deriveds

* fix: ensure SvelteMap reactivity persists through deriveds

* fix: ensure SvelteMap reactivity persists through deriveds
pull/13891/head
Dominic Gannaway 11 months ago committed by GitHub
parent 041e563466
commit 5a54ad9cc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure SvelteMap reactivity persists through deriveds

@ -94,13 +94,22 @@ export class SvelteMap extends Map {
var s = sources.get(key);
var prev_res = super.get(key);
var res = super.set(key, value);
var version = this.#version;
if (s === undefined) {
sources.set(key, source(0));
set(this.#size, super.size);
increment(this.#version);
increment(version);
} else if (prev_res !== value) {
increment(s);
// If no one listening to this property and is listening to the version, or
// the inverse, then we should increment the version to be safe
if (
(s.reactions === null && version.reactions !== null) ||
(s.reactions !== null && version.reactions === null)
) {
increment(version);
}
}
return res;

@ -0,0 +1,13 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
html: `Loading`,
async test({ assert, target }) {
await Promise.resolve();
flushSync();
assert.htmlEqual(target.innerHTML, `1`);
}
});

@ -0,0 +1,34 @@
<script>
import { untrack } from 'svelte';
import { SvelteMap } from 'svelte/reactivity';
const cache = new SvelteMap();
function get_async(id) {
const model = cache.get(id);
if (!model) {
const promise = new Promise(async () => {
await Promise.resolve();
cache.set(id, id.toString());
}).then(() => cache.get(id));
untrack(() => {
cache.set(id, promise);
});
return promise;
}
return model;
}
const value = $derived(get_async(1));
</script>
{#if value instanceof Promise}
Loading
{:else}
{value}
{/if}
Loading…
Cancel
Save