revise revise-this

pull/10951/head
Dominic Gannaway 8 months ago
parent cb18f8fbbe
commit 3683b8e120

@ -1,5 +1,5 @@
import { STATE_SYMBOL } from '../../../constants.js'; import { STATE_SYMBOL } from '../../../constants.js';
import { effect, render_effect } from '../../../reactivity/effects.js'; import { destroy_effect, effect, render_effect } from '../../../reactivity/effects.js';
import { untrack } from '../../../runtime.js'; import { untrack } from '../../../runtime.js';
/** /**
@ -22,38 +22,34 @@ function is_bound_this(bound_value, element_or_component) {
* @returns {void} * @returns {void}
*/ */
export function bind_this(element_or_component, update, get_value, get_parts) { export function bind_this(element_or_component, update, get_value, get_parts) {
effect(() => { /** @type {unknown[]} */
/** @type {unknown[]} */ var old_parts;
var old_parts;
/** @type {unknown[]} */ /** @type {unknown[]} */
var parts; var parts;
render_effect(() => { render_effect(() => {
old_parts = parts; old_parts = parts;
// We only track changes to the parts, not the value itself to avoid unnecessary reruns. // We only track changes to the parts, not the value itself to avoid unnecessary reruns.
parts = get_parts?.() || []; parts = get_parts?.() || [];
untrack(() => { untrack(() => {
if (element_or_component !== get_value(...parts)) { if (element_or_component !== get_value(...parts)) {
update(element_or_component, ...parts); update(element_or_component, ...parts);
// If this is an effect rerun (cause: each block context changes), then nullfiy the binding at // If this is an effect rerun (cause: each block context changes), then nullfiy the binding at
// the previous position if it isn't already taken over by a different effect. // the previous position if it isn't already taken over by a different effect.
if (old_parts && is_bound_this(get_value(...old_parts), element_or_component)) { if (old_parts && is_bound_this(get_value(...old_parts), element_or_component)) {
update(null, ...old_parts); update(null, ...old_parts);
}
} }
}); }
}); });
});
effect(() => {
return () => { return () => {
// Defer to the next tick so that all updates can be reconciled first. if (parts && is_bound_this(get_value(...parts), element_or_component)) {
// This solves the case where one variable is shared across multiple this-bindings. update(null, ...parts);
effect(() => { }
if (parts && is_bound_this(get_value(...parts), element_or_component)) {
update(null, ...parts);
}
});
}; };
}); });
} }

Loading…
Cancel
Save