Skip to content

Commit 41a02cc

Browse files
committed
Bug: Defer Disconnect Teardown
1 parent fdc9255 commit 41a02cc

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

packages/component/src/engines/native/base.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,16 @@ class WebComponentBase extends HTMLElementBase {
209209
}
210210

211211
disconnectedCallback() {
212-
if (this.template) {
212+
if (!this.template) { return; }
213+
// disconnect+connect in one task is a move, not a removal — defer
214+
// teardown so block hydration relocating nested components is safe.
215+
queueMicrotask(() => {
216+
if (this.isConnected || !this.template) { return; }
213217
this.template.onDestroyed();
214218
delete this.template;
215219
delete this.component;
216220
delete this.dataContext;
217-
}
221+
});
218222
}
219223

220224
attributeChangedCallback(attribute, oldValue, newValue) {

packages/component/test/browser/component.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ describe('Component', () => {
306306
expect(signal).toBeDefined();
307307
});
308308

309-
it('should handle disconnectedCallback correctly', () => {
309+
it('should handle disconnectedCallback correctly', async () => {
310310
const mockTemplate = {
311311
onDestroyed: vi.fn(),
312312
};
@@ -322,6 +322,7 @@ describe('Component', () => {
322322
instance.template = mockTemplate;
323323

324324
instance.disconnectedCallback();
325+
await Promise.resolve();
325326

326327
// Instance template's onDestroyed should be called
327328
expect(mockTemplate.onDestroyed).toHaveBeenCalled();
@@ -561,7 +562,7 @@ describe('Component', () => {
561562
document.body.appendChild(el);
562563
await rendered;
563564
document.body.removeChild(el);
564-
// synchronous in disconnectedCallback
565+
await Promise.resolve();
565566
expect(onDestroyed).toHaveBeenCalledTimes(1);
566567
expect(heard).toHaveBeenCalledTimes(1);
567568
});
@@ -852,6 +853,7 @@ describe('Component', () => {
852853
const signal = el.template.abortSignal;
853854
expect(signal.aborted).toBe(false);
854855
document.body.removeChild(el);
856+
await Promise.resolve();
855857
expect(signal.aborted).toBe(true);
856858
});
857859
});

0 commit comments

Comments
 (0)