0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-24 16:29:46 +01:00

fix: use strict equality for key block comparisons in runes mode (#14285)

fixes #14283
This commit is contained in:
Rich Harris 2024-11-13 03:24:56 -05:00 committed by GitHub
parent 25d9aa1828
commit ac9b7de058
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: use strict equality for key block comparisons in runes mode

View File

@ -1,7 +1,8 @@
/** @import { Effect, TemplateNode } from '#client' */ /** @import { Effect, TemplateNode } from '#client' */
import { UNINITIALIZED } from '../../../../constants.js'; import { UNINITIALIZED } from '../../../../constants.js';
import { block, branch, pause_effect } from '../../reactivity/effects.js'; import { block, branch, pause_effect } from '../../reactivity/effects.js';
import { safe_not_equal } from '../../reactivity/equality.js'; import { not_equal, safe_not_equal } from '../../reactivity/equality.js';
import { is_runes } from '../../runtime.js';
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js'; import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
/** /**
@ -24,8 +25,10 @@ export function key_block(node, get_key, render_fn) {
/** @type {Effect} */ /** @type {Effect} */
var effect; var effect;
var changed = is_runes() ? not_equal : safe_not_equal;
block(() => { block(() => {
if (safe_not_equal(key, (key = get_key()))) { if (changed(key, (key = get_key()))) {
if (effect) { if (effect) {
pause_effect(effect); pause_effect(effect);
} }

View File

@ -15,6 +15,15 @@ export function safe_not_equal(a, b) {
: a !== b || (a !== null && typeof a === 'object') || typeof a === 'function'; : a !== b || (a !== null && typeof a === 'object') || typeof a === 'function';
} }
/**
* @param {unknown} a
* @param {unknown} b
* @returns {boolean}
*/
export function not_equal(a, b) {
return a !== b;
}
/** @type {Equals} */ /** @type {Equals} */
export function safe_equals(value) { export function safe_equals(value) {
return !safe_not_equal(value, this.v); return !safe_not_equal(value, this.v);

View File

@ -0,0 +1,13 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
test({ assert, target, logs }) {
assert.deepEqual(logs, ['rendering']);
const btn = target.querySelector('button');
flushSync(() => btn?.click());
assert.deepEqual(logs, ['rendering']);
}
});

View File

@ -0,0 +1,10 @@
<script>
let inner = Symbol();
let outer = $state({ inner });
</script>
<button onclick={() => outer = { inner }}>update</button>
{#key outer.inner}
{console.log('rendering')}
{/key}