-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: run blocks eagerly during flush #16631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
66396f3
2048f5d
acc7bb3
f89e355
12ceae1
d5ba21f
c9b2654
87e5d00
8e2cc02
5d6b081
e7cc0e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: only abort effect flushing if it causes an existing effect to be scheduled | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,12 +292,12 @@ export class Batch { | |
if (!skip && effect.fn !== null) { | ||
if (is_branch) { | ||
effect.f ^= CLEAN; | ||
} else if ((flags & EFFECT) !== 0) { | ||
this.#effects.push(effect); | ||
} else if (async_mode_flag && (flags & RENDER_EFFECT) !== 0) { | ||
this.#render_effects.push(effect); | ||
} else if ((flags & CLEAN) === 0) { | ||
if ((flags & EFFECT) !== 0) { | ||
this.#effects.push(effect); | ||
} else if (async_mode_flag && (flags & RENDER_EFFECT) !== 0) { | ||
this.#render_effects.push(effect); | ||
} else if ((flags & ASYNC) !== 0) { | ||
if ((flags & ASYNC) !== 0) { | ||
var effects = effect.b?.pending ? this.#boundary_async_effects : this.#async_effects; | ||
effects.push(effect); | ||
} else if (is_dirty(effect)) { | ||
|
@@ -584,6 +584,9 @@ function infinite_loop_guard() { | |
} | ||
} | ||
|
||
/** @type {Effect[] | null} */ | ||
export let eager_block_effects = null; | ||
|
||
/** | ||
* @param {Array<Effect>} effects | ||
* @returns {void} | ||
|
@@ -598,7 +601,7 @@ function flush_queued_effects(effects) { | |
var effect = effects[i++]; | ||
|
||
if ((effect.f & (DESTROYED | INERT)) === 0 && is_dirty(effect)) { | ||
var n = current_batch ? current_batch.current.size : 0; | ||
eager_block_effects = []; | ||
|
||
update_effect(effect); | ||
|
||
|
@@ -619,21 +622,20 @@ function flush_queued_effects(effects) { | |
} | ||
} | ||
|
||
// if state is written in a user effect, abort and re-schedule, lest we run | ||
// effects that should be removed as a result of the state change | ||
if ( | ||
current_batch !== null && | ||
current_batch.current.size > n && | ||
(effect.f & USER_EFFECT) !== 0 | ||
) { | ||
break; | ||
if (eager_block_effects.length > 0) { | ||
// TODO this feels incorrect! it gets the tests passing | ||
old_values.clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the correct place since it's equivalent timing to before: previously you would bail the loop and start a new flush. That means you ended up at the end of the |
||
|
||
for (const e of eager_block_effects) { | ||
update_effect(e); | ||
} | ||
|
||
eager_block_effects = []; | ||
} | ||
} | ||
} | ||
|
||
while (i < length) { | ||
schedule_effect(effects[i++]); | ||
} | ||
eager_block_effects = null; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ import * as e from '../errors.js'; | |
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js'; | ||
import { get_stack, tag_proxy } from '../dev/tracing.js'; | ||
import { component_context, is_runes } from '../context.js'; | ||
import { Batch, schedule_effect } from './batch.js'; | ||
import { Batch, eager_block_effects, schedule_effect } from './batch.js'; | ||
import { proxy } from '../proxy.js'; | ||
import { execute_derived } from './deriveds.js'; | ||
|
||
|
@@ -334,6 +334,12 @@ function mark_reactions(signal, status) { | |
if ((flags & DERIVED) !== 0) { | ||
mark_reactions(/** @type {Derived} */ (reaction), MAYBE_DIRTY); | ||
} else if (not_dirty) { | ||
if ((flags & BLOCK_EFFECT) !== 0) { | ||
if (eager_block_effects !== null) { | ||
eager_block_effects.push(/** @type {Effect} */ (reaction)); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My guess for why this is necessary is that in the next batch that was scheduled due to this we need it for traversal of the graph. |
||
|
||
schedule_effect(/** @type {Effect} */ (reaction)); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let { children } = $props(); | ||
|
||
let inited = $state(false); | ||
|
||
$effect(() => { | ||
inited = true; | ||
}); | ||
</script> | ||
|
||
{#if inited} | ||
<span>{@render children()}</span> | ||
{/if} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
async test({ assert, target }) { | ||
const [button] = target.querySelectorAll('button'); | ||
|
||
assert.doesNotThrow(() => { | ||
flushSync(() => button.click()); | ||
}); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script> | ||
import Child from './Child.svelte'; | ||
|
||
let show = $state(false); | ||
</script> | ||
|
||
<button onclick={() => show = !show}> | ||
toggle | ||
</button> | ||
|
||
{#if show} | ||
{#each { length: 1234 } as i} | ||
<Child>{i}</Child> | ||
{/each} | ||
{/if} |
Uh oh!
There was an error while loading. Please reload this page.