Skip to content

Commit 1917982

Browse files
authored
create effect root for custom element (#13312)
1 parent 1d1e4ae commit 1917982

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

packages/svelte/src/internal/client/dom/elements/custom-element.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createClassComponent } from '../../../../legacy/legacy-client.js';
2-
import { destroy_effect, render_effect } from '../../reactivity/effects.js';
2+
import { destroy_effect, effect_root, render_effect } from '../../reactivity/effects.js';
33
import { append } from '../template.js';
44
import { define_property, get_descriptor, object_keys } from '../../../shared/utils.js';
55

@@ -145,24 +145,26 @@ if (typeof HTMLElement === 'function') {
145145
});
146146

147147
// Reflect component props as attributes
148-
this.$$me = render_effect(() => {
149-
this.$$r = true;
150-
for (const key of object_keys(this.$$c)) {
151-
if (!this.$$p_d[key]?.reflect) continue;
152-
this.$$d[key] = this.$$c[key];
153-
const attribute_value = get_custom_element_value(
154-
key,
155-
this.$$d[key],
156-
this.$$p_d,
157-
'toAttribute'
158-
);
159-
if (attribute_value == null) {
160-
this.removeAttribute(this.$$p_d[key].attribute || key);
161-
} else {
162-
this.setAttribute(this.$$p_d[key].attribute || key, attribute_value);
148+
this.$$me = effect_root(() => {
149+
render_effect(() => {
150+
this.$$r = true;
151+
for (const key of object_keys(this.$$c)) {
152+
if (!this.$$p_d[key]?.reflect) continue;
153+
this.$$d[key] = this.$$c[key];
154+
const attribute_value = get_custom_element_value(
155+
key,
156+
this.$$d[key],
157+
this.$$p_d,
158+
'toAttribute'
159+
);
160+
if (attribute_value == null) {
161+
this.removeAttribute(this.$$p_d[key].attribute || key);
162+
} else {
163+
this.setAttribute(this.$$p_d[key].attribute || key, attribute_value);
164+
}
163165
}
164-
}
165-
this.$$r = false;
166+
this.$$r = false;
167+
});
166168
});
167169

168170
for (const type in this.$$l) {
@@ -196,7 +198,7 @@ if (typeof HTMLElement === 'function') {
196198
Promise.resolve().then(() => {
197199
if (!this.$$cn && this.$$c) {
198200
this.$$c.$destroy();
199-
destroy_effect(this.$$me);
201+
this.$$me();
200202
this.$$c = undefined;
201203
}
202204
});

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,11 @@ function flush_queued_root_effects(root_effects) {
516516
effect.f ^= EFFECT_QUEUED;
517517
}
518518

519-
// When working with custom elements, the root effects might not have a root
520-
if (effect.first === null && (effect.f & BRANCH_EFFECT) === 0) {
521-
flush_queued_effects([effect]);
522-
} else {
523-
/** @type {Effect[]} */
524-
var collected_effects = [];
519+
/** @type {Effect[]} */
520+
var collected_effects = [];
525521

526-
process_effects(effect, collected_effects);
527-
flush_queued_effects(collected_effects);
528-
}
522+
process_effects(effect, collected_effects);
523+
flush_queued_effects(collected_effects);
529524
}
530525
} finally {
531526
is_flushing_effect = previously_flushing_effect;

0 commit comments

Comments
 (0)