Skip to content

Commit 0b1a78a

Browse files
committed
alternative approach
1 parent 5156964 commit 0b1a78a

File tree

4 files changed

+16
-29
lines changed

4 files changed

+16
-29
lines changed

packages/svelte/src/internal/client/dom/elements/bindings/this.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { STATE_SYMBOL } from '../../../constants.js';
2-
import { branch, effect, render_effect } from '../../../reactivity/effects.js';
3-
import {
4-
current_effect,
5-
current_reaction,
6-
set_current_effect,
7-
set_current_reaction,
8-
untrack
9-
} from '../../../runtime.js';
2+
import { effect, render_effect } from '../../../reactivity/effects.js';
3+
import { untrack } from '../../../runtime.js';
4+
import { queue_task } from '../../task.js';
105

116
/**
127
* @param {any} bound_value
@@ -53,18 +48,12 @@ export function bind_this(element_or_component, update, get_value, get_parts) {
5348
});
5449

5550
return () => {
56-
const previous_effect = current_effect;
57-
const previous_reaction = current_reaction;
58-
// TODO: maybe we should use something other an effect branch here to emulate the microtask behaviour.
59-
set_current_effect(null);
60-
set_current_reaction(null);
61-
branch(() => {
51+
// We cannot use effects in the teardown phase, we we use a microtask instead.
52+
queue_task(() => {
6253
if (parts && is_bound_this(get_value(...parts), element_or_component)) {
6354
update(null, ...parts);
6455
}
6556
});
66-
set_current_effect(previous_effect);
67-
set_current_reaction(previous_reaction);
6857
};
6958
});
7059
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { run_all } from '../../shared/utils.js';
22

33
let is_task_queued = false;
4-
let is_raf_queued = false;
54

65
/** @type {Array<() => void>} */
76
let current_queued_tasks = [];
8-
/** @type {Array<() => void>} */
9-
let current_raf_tasks = [];
107

118
function process_task() {
129
is_task_queued = false;
@@ -15,11 +12,15 @@ function process_task() {
1512
run_all(tasks);
1613
}
1714

18-
function process_raf_task() {
19-
is_raf_queued = false;
20-
const tasks = current_raf_tasks.slice();
21-
current_raf_tasks = [];
22-
run_all(tasks);
15+
/**
16+
* @param {() => void} fn
17+
*/
18+
export function queue_task(fn) {
19+
if (!is_task_queued) {
20+
is_task_queued = true;
21+
queueMicrotask(process_task);
22+
}
23+
current_queued_tasks.push(fn);
2324
}
2425

2526
/**
@@ -29,7 +30,4 @@ export function flush_tasks() {
2930
if (is_task_queued) {
3031
process_task();
3132
}
32-
if (is_raf_queued) {
33-
process_raf_task();
34-
}
3533
}

packages/svelte/src/internal/client/runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,11 @@ export function flush_sync(fn, flush_previous = true) {
669669

670670
var result = fn?.();
671671

672+
flush_tasks();
672673
if (current_queued_root_effects.length > 0 || root_effects.length > 0) {
673674
flush_sync();
674675
}
675676

676-
flush_tasks();
677677
flush_count = 0;
678678

679679
return result;

packages/svelte/tests/runtime-legacy/samples/ondestroy-before-cleanup/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export default test({
88
const div = target.querySelector('div');
99

1010
component.visible = false;
11-
assert.equal(container.div, null);
11+
assert.equal(container.div, div);
1212
}
1313
});

0 commit comments

Comments
 (0)