Skip to content

Commit

Permalink
fix: other blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti committed Feb 21, 2025
1 parent 8bd65ca commit c2c2edd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function AwaitBlock(node, context) {
if (node.then) {
const then_context = {
...context,
state: { ...context.state, transform: { ...context.state.transform } }
state: { ...context.state, transform: { ...context.state.transform }, needs_safe_props: true }
};
const argument = node.value && create_derived_block_argument(node.value, then_context);

Expand All @@ -37,7 +37,7 @@ export function AwaitBlock(node, context) {
}

if (node.catch) {
const catch_context = { ...context, state: { ...context.state } };
const catch_context = { ...context, state: { ...context.state, needs_safe_props: true } };
const argument = node.error && create_derived_block_argument(node.error, catch_context);

/** @type {Pattern[]} */
Expand All @@ -59,7 +59,12 @@ export function AwaitBlock(node, context) {
context.state.node,
expression,
node.pending
? b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.pending)))
? b.arrow(
[b.id('$$anchor')],
/** @type {BlockStatement} */ (
context.visit(node.pending, { ...context.state, needs_safe_props: true })
)
)
: b.literal(null),
then_block,
catch_block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ export function EachBlock(node, context) {
const child_state = {
...context.state,
transform: { ...context.state.transform },
store_to_invalidate
store_to_invalidate,
needs_safe_props: true
};

/** The state used when generating the key function, if necessary */
Expand Down Expand Up @@ -308,7 +309,15 @@ export function EachBlock(node, context) {

if (node.fallback) {
args.push(
b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.fallback)))
b.arrow(
[b.id('$$anchor')],
/** @type {BlockStatement} */ (
context.visit(node.fallback, {
...context.state,
needs_safe_props: true
})
)
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export function KeyBlock(node, context) {
context.state.template.push('<!>');

const key = /** @type {Expression} */ (context.visit(node.expression));
const body = /** @type {Expression} */ (context.visit(node.fragment));
const body = /** @type {Expression} */ (
context.visit(node.fragment, { ...context.state, needs_safe_props: true })
);

context.state.init.push(
b.stmt(b.call('$.key', context.state.node, b.thunk(key), b.arrow([b.id('$$anchor')], body)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function SnippetBlock(node, context) {
const declarations = [];

const transform = { ...context.state.transform };
const child_state = { ...context.state, transform };
const child_state = { ...context.state, transform, needs_safe_props: true };

for (let i = 0; i < node.parameters.length; i++) {
const argument = node.parameters[i];
Expand Down
75 changes: 32 additions & 43 deletions packages/svelte/src/internal/client/reactivity/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,17 @@ import {
PROPS_IS_RUNES,
PROPS_IS_UPDATED
} from '../../../constants.js';
import { legacy_mode_flag } from '../../flags/index.js';
import { get_descriptor, is_function } from '../../shared/utils.js';
import { mutable_source, set, source, update } from './sources.js';
import { derived, derived_safe_equal } from './deriveds.js';
import {
active_effect,
get,
captured_signals,
set_active_effect,
untrack,
active_reaction,
set_active_reaction
} from '../runtime.js';
import { safe_equals } from './equality.js';
import { LEGACY_DERIVED_PROP, LEGACY_PROPS, STATE_SYMBOL } from '../constants.js';
import * as e from '../errors.js';
import {
BRANCH_EFFECT,
LEGACY_DERIVED_PROP,
LEGACY_PROPS,
ROOT_EFFECT,
STATE_SYMBOL
} from '../constants.js';
import { proxy } from '../proxy.js';
import { capture_store_binding } from './store.js';
import { legacy_mode_flag } from '../../flags/index.js';
import { captured_signals, get, is_flushing_effect, untrack } from '../runtime.js';
import { derived, derived_safe_equal } from './deriveds.js';
import { teardown } from './effects.js';
import { safe_equals } from './equality.js';
import { inspect_effects, mutable_source, set, source, update } from './sources.js';
import { capture_store_binding } from './store.js';

/**
* @param {((value?: number) => number)} fn
Expand Down Expand Up @@ -428,28 +414,31 @@ export function safe_props(props) {
unmounting = true;
});
const deriveds = new Map();
/**
* @type {Map<string|symbol, unknown>}
*/
const olds = new Map(untrack(() => Object.entries(props)));
return new Proxy(
{},
{
get(_, key) {
if (!deriveds.has(key)) {
deriveds.set(
key,
derived(() => {
if (unmounting) {
return olds.get(key);
}
olds.set(key, props[key]);
return props[key];
})
);
return untrack(() => {
/**
* @type {Map<string|symbol, unknown>}
*/
const olds = new Map(Object.entries(props));

return new Proxy(
{},
{
get(_, key) {
if (!deriveds.has(key)) {
deriveds.set(
key,
derived(() => {
if (unmounting) {
return olds.get(key);
}
olds.set(key, props[key]);
return props[key];
})
);
}
return get(deriveds.get(key));
}
return get(deriveds.get(key));
}
}
);
);
});
}

0 comments on commit c2c2edd

Please sign in to comment.