make it unnecessary to hand onto `current_sources` past the initial update

pull/16333/head
Rich Harris 2 months ago
parent 38e6943ef5
commit 85e7439e24

@ -1,6 +1,13 @@
/** @import { Source } from '#client' */ /** @import { Source } from '#client' */
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { get, active_effect, current_sources, set_current_sources } from './runtime.js'; import {
get,
active_effect,
update_version,
active_reaction,
set_update_version,
set_active_reaction
} from './runtime.js';
import { import {
array_prototype, array_prototype,
get_descriptor, get_descriptor,
@ -41,7 +48,7 @@ export function proxy(value) {
var version = source(0); var version = source(0);
var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null; var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null;
var parent_sources = current_sources; var parent_version = update_version;
/** /**
* Executes the proxy in the context of the reaction it was originally created in, if any * Executes the proxy in the context of the reaction it was originally created in, if any
@ -49,13 +56,23 @@ export function proxy(value) {
* @param {() => T} fn * @param {() => T} fn
*/ */
var with_parent = (fn) => { var with_parent = (fn) => {
var previous_sources = current_sources; if (update_version === parent_version) {
set_current_sources(parent_sources); return fn();
}
// child source is being created after the initial proxy —
// prevent it from being associated with the current reaction
var reaction = active_reaction;
var version = update_version;
set_active_reaction(null);
set_update_version(parent_version);
/** @type {T} */
var result = fn(); var result = fn();
set_current_sources(previous_sources); set_active_reaction(reaction);
set_update_version(version);
return result; return result;
}; };

@ -92,13 +92,6 @@ export function set_active_effect(effect) {
*/ */
export let current_sources = null; export let current_sources = null;
/**
* @param {null | Source[]} value
*/
export function set_current_sources(value) {
current_sources = value;
}
/** @param {Value} value */ /** @param {Value} value */
export function push_reaction_value(value) { export function push_reaction_value(value) {
if (active_reaction !== null && active_reaction.f & EFFECT_IS_UPDATING) { if (active_reaction !== null && active_reaction.f & EFFECT_IS_UPDATING) {
@ -143,6 +136,11 @@ let read_version = 0;
export let update_version = read_version; export let update_version = read_version;
/** @param {number} value */
export function set_update_version(value) {
update_version = value;
}
// If we are working with a get() chain that has no active container, // If we are working with a get() chain that has no active container,
// to prevent memory leaks, we skip adding the reaction. // to prevent memory leaks, we skip adding the reaction.
export let skip_reaction = false; export let skip_reaction = false;

Loading…
Cancel
Save