Skip to content

Commit

Permalink
alternative approach
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm committed Apr 17, 2024
1 parent 5156964 commit 0b1a78a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
21 changes: 5 additions & 16 deletions packages/svelte/src/internal/client/dom/elements/bindings/this.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { STATE_SYMBOL } from '../../../constants.js';
import { branch, effect, render_effect } from '../../../reactivity/effects.js';
import {
current_effect,
current_reaction,
set_current_effect,
set_current_reaction,
untrack
} from '../../../runtime.js';
import { effect, render_effect } from '../../../reactivity/effects.js';
import { untrack } from '../../../runtime.js';
import { queue_task } from '../../task.js';

/**
* @param {any} bound_value
Expand Down Expand Up @@ -53,18 +48,12 @@ export function bind_this(element_or_component, update, get_value, get_parts) {
});

return () => {
const previous_effect = current_effect;
const previous_reaction = current_reaction;
// TODO: maybe we should use something other an effect branch here to emulate the microtask behaviour.
set_current_effect(null);
set_current_reaction(null);
branch(() => {
// We cannot use effects in the teardown phase, we we use a microtask instead.
queue_task(() => {
if (parts && is_bound_this(get_value(...parts), element_or_component)) {
update(null, ...parts);
}
});
set_current_effect(previous_effect);
set_current_reaction(previous_reaction);
};
});
}
20 changes: 9 additions & 11 deletions packages/svelte/src/internal/client/dom/task.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { run_all } from '../../shared/utils.js';

let is_task_queued = false;
let is_raf_queued = false;

/** @type {Array<() => void>} */
let current_queued_tasks = [];
/** @type {Array<() => void>} */
let current_raf_tasks = [];

function process_task() {
is_task_queued = false;
Expand All @@ -15,11 +12,15 @@ function process_task() {
run_all(tasks);
}

function process_raf_task() {
is_raf_queued = false;
const tasks = current_raf_tasks.slice();
current_raf_tasks = [];
run_all(tasks);
/**
* @param {() => void} fn
*/
export function queue_task(fn) {
if (!is_task_queued) {
is_task_queued = true;
queueMicrotask(process_task);
}
current_queued_tasks.push(fn);
}

/**
Expand All @@ -29,7 +30,4 @@ export function flush_tasks() {
if (is_task_queued) {
process_task();
}
if (is_raf_queued) {
process_raf_task();
}
}
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 @@ -669,11 +669,11 @@ export function flush_sync(fn, flush_previous = true) {

var result = fn?.();

flush_tasks();
if (current_queued_root_effects.length > 0 || root_effects.length > 0) {
flush_sync();
}

flush_tasks();
flush_count = 0;

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default test({
const div = target.querySelector('div');

component.visible = false;
assert.equal(container.div, null);
assert.equal(container.div, div);
}
});

0 comments on commit 0b1a78a

Please sign in to comment.