Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-seas-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure guards (eg. if, each, key) run before their contents
23 changes: 20 additions & 3 deletions packages/svelte/src/internal/client/reactivity/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,15 +601,32 @@ function flush_queued_effects(effects) {
// If update_effect() has a flushSync() in it, we may have flushed another flush_queued_effects(),
// which already handled this logic and did set eager_block_effects to null.
if (eager_block_effects?.length > 0) {
// TODO this feels incorrect! it gets the tests passing
old_values.clear();

/** @type {Array<{ effect: Effect; depth: number }>} */
const effects_with_depth = [];
for (const e of eager_block_effects) {
update_effect(e);
// Skip eager effects that have already been unmounted
if ((e.f & (DESTROYED | INERT)) !== 0) continue;

let depth = 0;
let ancestor = e.parent;
while (ancestor !== null) {
depth++;
ancestor = ancestor.parent;
}

effects_with_depth.push({ effect: e, depth });
}

eager_block_effects = [];
effects_with_depth.sort((a, b) => a.depth - b.depth);

for (const { effect } of effects_with_depth) {
update_effect(effect);
}
}

eager_block_effects = [];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, vi } from 'vitest';
import { test } from '../../test';
import { flushSync } from 'svelte';

const trackBranch = vi.fn();

export default test({
mode: ['client'],
props: { trackBranch: trackBranch },
async test({ target }) {
const button = target.querySelector('button');

flushSync(() => button?.click());
flushSync(() => button?.click());
flushSync(() => button?.click());
flushSync(() => button?.click());

expect(trackBranch).toHaveBeenCalledWith('one');
expect(trackBranch).toHaveBeenCalledWith('two');
expect(trackBranch).not.toHaveBeenCalledWith('else');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script lang="ts">
import {untrack} from 'svelte';

const {trackBranch}:{trackBranch: (branch:string)=>void} = $props();

let b = $state(false);
let v = $state("two");


$effect(() => {
v = b ? "one" : "two";
})
</script>


<button onclick={() => b = !b}>Trigger</button>


{#if v === "one"}
<div>if1 matched! {trackBranch('one')}</div>
{:else if v === "two"}
<div>if2 matched! {trackBranch('two')}</div>
{:else}
<div>nothing matched {trackBranch('else')}</div>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test } from '../../test';
import { flushSync } from 'svelte';

export default test({
mode: ['client'],
async test({ target, assert }) {
const button = target.querySelector('button');

flushSync(() => button?.click());

assert.equal(target.textContent?.trim(), 'Trigger');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
let centerRow = $state({ nested: { optional: 2, required: 3 } },
);

let someChange = $state(false);
$effect(() => {
if (someChange) centerRow = undefined;
});
</script>

{#if centerRow?.nested}
{#if centerRow?.nested?.optional != undefined && centerRow.nested.optional > 0}
op: {centerRow.nested.optional}<br />
{:else}
req: {centerRow.nested.required}<br />
{/if}
{/if}

<button onclick={() => (someChange = true)}>Trigger</button>
Loading