Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: throw an error when trying to subclass BrowserWindow #41795

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

codebytere
Copy link
Member

@codebytere codebytere commented Apr 8, 2024

Description of Change

Closes #41785.

Explicitly throws an error when users try to subclass BrowserWindow as it causes issues over the binding / lifecycle boundaries.

Also adds some tests.

Checklist

Release Notes

Notes: Fixed an issue where BrowserWindow.getAllWindows() did not return subclasses of BrowserWindow.

@codebytere codebytere added semver/patch backwards-compatible bug fixes target/28-x-y PR should also be added to the "28-x-y" branch. target/29-x-y PR should also be added to the "29-x-y" branch. target/30-x-y PR should also be added to the "30-x-y" branch. labels Apr 8, 2024
@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Apr 8, 2024
@MarshallOfSound
Copy link
Member

This has come up before (will find ref in the am) and this is intentional, subclassing any of electrons built-in classes is not supported and will cause various issues, mostly over the binding / lifecycle boundaries. I think we even explicitly document somewhere not to subclass our api

@codebytere
Copy link
Member Author

ah gotcha - we should document that more visibly then i'd say as i checked around a bit and couldn't find anything in the ts commit history about intentionality of it

@nornagon
Copy link
Member

nornagon commented Apr 8, 2024

We should probably make BrowserWindow's constructor throw if it's being instantiated as a subclass.

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Apr 9, 2024
@codebytere codebytere changed the title fix: subclasses should show in BrowserWindow.getAllWindows() chore: throw an error when trying to subclass BrowserWindow Apr 9, 2024
@codebytere codebytere requested review from nornagon and MarshallOfSound and removed request for zcbenz April 9, 2024 20:55
@codebytere
Copy link
Member Author

@nornagon done - if you have a better idea of where I should throw let me know but trying in BrowserWindow::BrowserWindow via GetConstructorName() looked like it'd be a headache so i went with _init.

Comment on lines +8 to +9
if (this.constructor.name !== 'BrowserWindow') {
throw new Error('Subclassing BrowserWindow is not supported in Electron');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I think this actually has to happen in C++. By the time _init is called, a bunch of BW initialization has already happened. And I don't think we correctly handle thrown errors in _init—we continue initializing the window regardless. I'm a little surprised the test passes!

@github-actions github-actions bot added the target/31-x-y PR should also be added to the "31-x-y" branch. label Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch backwards-compatible bug fixes target/28-x-y PR should also be added to the "28-x-y" branch. target/29-x-y PR should also be added to the "29-x-y" branch. target/30-x-y PR should also be added to the "30-x-y" branch. target/31-x-y PR should also be added to the "31-x-y" branch.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Subclass of BrowserWindow missing from BrowserWindow.getAllWindows()
3 participants