create effect root for custom element (#13312)

pull/13311/head
Rich Harris 2 months ago committed by GitHub
parent 1d1e4aeeb1
commit 19179822c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,5 +1,5 @@
import { createClassComponent } from '../../../../legacy/legacy-client.js'; import { createClassComponent } from '../../../../legacy/legacy-client.js';
import { destroy_effect, render_effect } from '../../reactivity/effects.js'; import { destroy_effect, effect_root, render_effect } from '../../reactivity/effects.js';
import { append } from '../template.js'; import { append } from '../template.js';
import { define_property, get_descriptor, object_keys } from '../../../shared/utils.js'; import { define_property, get_descriptor, object_keys } from '../../../shared/utils.js';
@ -145,24 +145,26 @@ if (typeof HTMLElement === 'function') {
}); });
// Reflect component props as attributes // Reflect component props as attributes
this.$$me = render_effect(() => { this.$$me = effect_root(() => {
this.$$r = true; render_effect(() => {
for (const key of object_keys(this.$$c)) { this.$$r = true;
if (!this.$$p_d[key]?.reflect) continue; for (const key of object_keys(this.$$c)) {
this.$$d[key] = this.$$c[key]; if (!this.$$p_d[key]?.reflect) continue;
const attribute_value = get_custom_element_value( this.$$d[key] = this.$$c[key];
key, const attribute_value = get_custom_element_value(
this.$$d[key], key,
this.$$p_d, this.$$d[key],
'toAttribute' this.$$p_d,
); 'toAttribute'
if (attribute_value == null) { );
this.removeAttribute(this.$$p_d[key].attribute || key); if (attribute_value == null) {
} else { this.removeAttribute(this.$$p_d[key].attribute || key);
this.setAttribute(this.$$p_d[key].attribute || key, attribute_value); } else {
this.setAttribute(this.$$p_d[key].attribute || key, attribute_value);
}
} }
} this.$$r = false;
this.$$r = false; });
}); });
for (const type in this.$$l) { for (const type in this.$$l) {
@ -196,7 +198,7 @@ if (typeof HTMLElement === 'function') {
Promise.resolve().then(() => { Promise.resolve().then(() => {
if (!this.$$cn && this.$$c) { if (!this.$$cn && this.$$c) {
this.$$c.$destroy(); this.$$c.$destroy();
destroy_effect(this.$$me); this.$$me();
this.$$c = undefined; this.$$c = undefined;
} }
}); });

@ -516,16 +516,11 @@ function flush_queued_root_effects(root_effects) {
effect.f ^= EFFECT_QUEUED; effect.f ^= EFFECT_QUEUED;
} }
// When working with custom elements, the root effects might not have a root /** @type {Effect[]} */
if (effect.first === null && (effect.f & BRANCH_EFFECT) === 0) { var collected_effects = [];
flush_queued_effects([effect]);
} else {
/** @type {Effect[]} */
var collected_effects = [];
process_effects(effect, collected_effects); process_effects(effect, collected_effects);
flush_queued_effects(collected_effects); flush_queued_effects(collected_effects);
}
} }
} finally { } finally {
is_flushing_effect = previously_flushing_effect; is_flushing_effect = previously_flushing_effect;

Loading…
Cancel
Save