Skip to content

Commit cf822c2

Browse files
committed
Tweak to restore unsafe template mutation tracking
1 parent 50da926 commit cf822c2

File tree

2 files changed

+16
-5
lines changed
  • packages/svelte

2 files changed

+16
-5
lines changed

packages/svelte/src/internal/client/proxy.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
define_property
1111
} from '../shared/utils.js';
1212
import { state as source, set } from './reactivity/sources.js';
13-
import { PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
13+
import { PROXY_PATH_SYMBOL, STATE_SYMBOL, DERIVED, BLOCK_EFFECT } from '#client/constants';
1414
import { UNINITIALIZED } from '../../constants.js';
1515
import * as e from './errors.js';
1616
import { get_stack, tag } from './dev/tracing.js';
@@ -199,7 +199,9 @@ export function proxy(value) {
199199
// internals) that the algorithm reads.
200200
if (is_proxied_array && typeof prop === 'string' && MUTATING_ARRAY_METHODS.has(prop)) {
201201
/** @type {Map<string, Function>} */
202-
const mutating_methods_cache = /** @type {Map<string, Function>} */ (proxied_array_mutating_methods_cache);
202+
const mutating_methods_cache = /** @type {Map<string, Function>} */ (
203+
proxied_array_mutating_methods_cache
204+
);
203205

204206
var cached_method = mutating_methods_cache.get(prop);
205207

@@ -212,8 +214,17 @@ export function proxy(value) {
212214
*/
213215
cached_method = function (...args) {
214216
// preserve correct `this` binding and forward result.
215-
// eslint-disable-next-line prefer-spread
216-
return untrack(() => /** @type {any} */ (array_prototype)[prop].apply(this, args));
217+
const fn = /** @type {any} */ (array_prototype)[prop];
218+
219+
// if we are inside a template expression/derived or block effect,
220+
// keep tracking so the runtime can still detect unsafe mutations.
221+
if (active_reaction !== null && (active_reaction.f & (DERIVED | BLOCK_EFFECT)) !== 0) {
222+
return fn.apply(this, args);
223+
}
224+
225+
// otherwise (ordinary user effect etc.) suppress dependency tracking
226+
// so we avoid the self-invalidating loop.
227+
return untrack(() => fn.apply(this, args));
217228
};
218229

219230
// give the wrapper a meaningful name for better debugging

packages/svelte/tests/runtime-runes/samples/array-push-in-effect/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ export default test({
3030
`
3131
);
3232
}
33-
});
33+
});

0 commit comments

Comments
 (0)