Skip to content

feat: defer tasks without creating effects #11960

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

Merged
merged 8 commits into from
Jun 10, 2024
Merged
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/late-bees-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: defer tasks without creating effects
13 changes: 5 additions & 8 deletions packages/svelte/src/internal/client/dom/blocks/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import {
} from '../hydration.js';
import { clear_text_content, empty } from '../operations.js';
import { remove } from '../reconciler.js';
import { untrack } from '../../runtime.js';
import {
block,
branch,
destroy_effect,
effect,
run_out_transitions,
pause_children,
pause_effect,
Expand All @@ -31,6 +29,7 @@ import {
import { source, mutable_source, set } from '../../reactivity/sources.js';
import { is_array, is_frozen } from '../../utils.js';
import { INERT, STATE_SYMBOL } from '../../constants.js';
import { queue_micro_task } from '../task.js';

/**
* The row of a keyed each block that is currently updating. We track this
Expand Down Expand Up @@ -423,12 +422,10 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
}

if (is_animated) {
effect(() => {
untrack(() => {
for (item of to_animate) {
item.a?.apply();
}
});
queue_micro_task(() => {
for (item of to_animate) {
item.a?.apply();
}
});
}
}
Expand Down
23 changes: 7 additions & 16 deletions packages/svelte/src/internal/client/dom/elements/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import {
} from '../../../../constants.js';
import { create_event, delegate } from './events.js';
import { add_form_reset_listener, autofocus } from './misc.js';
import { effect, effect_root } from '../../reactivity/effects.js';
import * as w from '../../warnings.js';
import { LOADING_ATTR_SYMBOL } from '../../constants.js';
import { queue_idle_task } from '../task.js';
import { queue_idle_task, queue_micro_task } from '../task.js';

/**
* The value/checked attribute in the template actually corresponds to the defaultValue property, so we need
Expand Down Expand Up @@ -262,21 +261,13 @@ export function set_attributes(element, prev, next, lowercase_attributes, css_ha
// On the first run, ensure that events are added after bindings so
// that their listeners fire after the binding listeners
if (!prev) {
// In edge cases it may happen that set_attributes is re-run before the
// effect is executed. In that case the render effect which initiates this
// re-run will destroy the inner effect and it will never run. But because
// next and prev may have the same keys, the event would not get added again
// and it would get lost. We prevent this by using a root effect.
const destroy_root = effect_root(() => {
effect(() => {
if (!element.isConnected) return;
for (const [key, value, evt] of events) {
if (current[key] === value) {
evt();
}
queue_micro_task(() => {
if (!element.isConnected) return;
for (const [key, value, evt] of events) {
if (current[key] === value) {
evt();
}
destroy_root();
});
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DEV } from 'esm-env';
import { render_effect, effect, teardown } from '../../../reactivity/effects.js';
import { render_effect, teardown } from '../../../reactivity/effects.js';
import { listen_to_event_and_reset_event } from './shared.js';
import * as e from '../../../errors.js';
import { get_proxied_value, is } from '../../../proxy.js';
import { queue_micro_task } from '../../task.js';

/**
* @param {HTMLInputElement} input
Expand Down Expand Up @@ -111,7 +112,7 @@ export function bind_group(inputs, group_index, input, get_value, update) {
}
});

effect(() => {
queue_micro_task(() => {
// necessary to maintain binding group order in all insertion scenarios. TODO optimise
binding_group.sort((a, b) => (a.compareDocumentPosition(b) === 4 ? -1 : 1));
});
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dom/elements/events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render_effect, teardown } from '../../reactivity/effects.js';
import { teardown } from '../../reactivity/effects.js';
import { all_registered_events, root_event_handles } from '../../render.js';
import { define_property, is_array } from '../../utils.js';
import { hydrating } from '../hydration.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/dom/elements/misc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hydrating } from '../hydration.js';
import { effect } from '../../reactivity/effects.js';
import { clear_text_content } from '../operations.js';
import { queue_micro_task } from '../task.js';

/**
* @param {HTMLElement} dom
Expand All @@ -12,7 +12,7 @@ export function autofocus(dom, value) {
const body = document.body;
dom.autofocus = true;

effect(() => {
queue_micro_task(() => {
if (document.activeElement === body) {
dom.focus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { is_function } from '../../utils.js';
import { current_each_item } from '../blocks/each.js';
import { TRANSITION_GLOBAL, TRANSITION_IN, TRANSITION_OUT } from '../../../../constants.js';
import { BLOCK_EFFECT, EFFECT_RAN, EFFECT_TRANSPARENT } from '../../constants.js';
import { queue_micro_task } from '../task.js';

/**
* @param {Element} element
Expand Down Expand Up @@ -272,8 +273,8 @@ function animate(element, options, counterpart, t2, callback) {
/** @type {import('#client').Animation} */
var a;

effect(() => {
var o = untrack(() => options({ direction: t2 === 1 ? 'in' : 'out' }));
queue_micro_task(() => {
var o = options({ direction: t2 === 1 ? 'in' : 'out' });
a = animate(element, o, counterpart, t2, callback);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/dom/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { empty } from './operations.js';
import { create_fragment_from_html } from './reconciler.js';
import { current_effect } from '../runtime.js';
import { TEMPLATE_FRAGMENT, TEMPLATE_USE_IMPORT_NODE } from '../../../constants.js';
import { effect } from '../reactivity/effects.js';
import { is_array } from '../utils.js';
import { queue_micro_task } from './task.js';

/**
* @template {import("#client").TemplateNode | import("#client").TemplateNode[]} T
Expand Down Expand Up @@ -196,7 +196,7 @@ function run_scripts(node) {
// Don't do it in other circumstances or we could accidentally execute scripts
// in an adjacent @html tag that was instantiated in the meantime.
if (script === node) {
effect(() => script.replaceWith(clone));
queue_micro_task(() => script.replaceWith(clone));
} else {
script.replaceWith(clone);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ function infinite_loop_guard() {
* @returns {void}
*/
function flush_queued_root_effects(root_effects) {
const length = root_effects.length;
var length = root_effects.length;
if (length === 0) {
return;
}
Expand Down
Loading