Skip to content

Commit 9b69730

Browse files
authored
fix: avoid error when LynxView is removed immediately after connected (#2182)
<!-- Thank you for submitting a pull request! We appreciate the time and effort you have invested in making these changes. Please ensure that you provide enough information to allow others to review your pull request. Upon submission, your pull request will be automatically assigned with reviewers. If you want to learn more about contributing to this project, please visit: https://github.com/lynx-family/lynx-stack/blob/main/CONTRIBUTING.md. --> <!-- The AI summary below will be auto-generated - feel free to replace it with your own. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Prevented an error when a LynxView is removed immediately after being connected, improving stability during rapid lifecycle changes. * **Tests** * Added a test to ensure no errors occur when a view is connected and then immediately disconnected, preventing regressions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> ## Checklist <!--- Check and mark with an "x" --> - [ ] Tests updated (or not required). - [ ] Documentation updated (or not required). - [ ] Changeset added, and when a BREAKING CHANGE occurs, it needs to be clearly marked (or not required).
1 parent 3cdf389 commit 9b69730

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lynx-js/web-core": patch
3+
---
4+
5+
fix: avoid error when LynxView is removed immediately after connected

packages/web-platform/web-core/src/apis/LynxView.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export class LynxView extends HTMLElement {
9292
);
9393
#instance?: LynxViewInstance;
9494

95-
#connected = false;
9695
#url?: string;
9796
/**
9897
* @public
@@ -414,10 +413,13 @@ export class LynxView extends HTMLElement {
414413
* @private
415414
*/
416415
#render() {
417-
if (!this.#rendering && this.#connected) {
416+
if (!this.#rendering && this.isConnected) {
418417
this.#rendering = true;
419418
queueMicrotask(() => {
420419
this.#rendering = false;
420+
if (!this.isConnected) {
421+
return;
422+
}
421423
const ssrData = this.getAttribute('ssr');
422424
if (this.#instance) {
423425
this.disconnectedCallback();
@@ -529,7 +531,6 @@ export class LynxView extends HTMLElement {
529531
* @private
530532
*/
531533
connectedCallback() {
532-
this.#connected = true;
533534
this.#render();
534535
}
535536
}

packages/web-platform/web-tests/tests/web-core.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,4 +713,40 @@ test.describe('web core tests', () => {
713713
await wait(1000);
714714
expect(isSuccess).toBeTruthy();
715715
});
716+
test('should not throw error when removed immediately after connected', async ({ page }) => {
717+
const errors: Error[] = [];
718+
page.on('pageerror', (err) => {
719+
errors.push(err);
720+
});
721+
722+
await goto(page);
723+
724+
// Evaluate in browser context
725+
await page.evaluate(async () => {
726+
// Verify DOM behavior assumption
727+
const div = document.createElement('div');
728+
const shadow = div.attachShadow({ mode: 'open' });
729+
const style = document.createElement('style');
730+
shadow.appendChild(style);
731+
document.body.appendChild(div);
732+
if (!style.sheet) throw new Error('Sheet should exist when connected');
733+
734+
document.body.removeChild(div);
735+
if (style.sheet) {
736+
throw new Error('Sheet should be null when disconnected');
737+
}
738+
739+
// Create a new lynx-view
740+
const view = document.createElement('lynx-view');
741+
// Connect it
742+
document.body.appendChild(view);
743+
// Immediately disconnect it
744+
document.body.removeChild(view);
745+
746+
// Wait a bit to ensure microtasks run
747+
await new Promise((resolve) => setTimeout(resolve, 100));
748+
});
749+
750+
expect(errors.length).toBe(0);
751+
});
716752
});

0 commit comments

Comments
 (0)