Skip to content

Conversation

EliasWatson
Copy link

pixi-react does not work inside of a new window using react-new-window. This is something that previously worked in pixi-react 7. The reason it is broken is the usage of the instanceof operator. That operator does not work when using multiple JS realms (See MDN). Unfortunately there is no airtight way to check types when using multiple realms. The best you can do is check some properties that are unique to that class.

I have tested my changes on the enterprise app I maintain and everything now seems to be working in a new window.

Copy link
Collaborator

@thejustinwalsh thejustinwalsh left a comment

Choose a reason for hiding this comment

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

Hey, thanks for this PR and highlighting this issue.

My concerns lie with false positives of class types within Pixi and ensureing we are being exhaustive of all subclasses of container.

In languages without runtime type checking, the simplest way to resolve this issue is to tag or brand the class with data to do the comparison reliably.

We should move forward with this PR (after review) to unblock, and draft a PR to core pixi to add a fallback for type checking accross realms benifiting the greater pixi community as well -- A reasonably reliable solution that works across realms and would have low false positives would be to add a static type field to the core pixi classes that uses Symbol.for to create a global symbol for each pixi class, then walk the prototype chain and compare instance.constructor.type to the global symbol for a container.

Comment on lines 122 to 125
return typeof obj.nodeName === 'string'
&& typeof obj.nodeType === 'number'
&& obj.nodeType === 1 // Element node
&& typeof obj.style === 'object';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would this check suffice?

return obj.nodeType === Node.ELEMENT_NODE || (obj.nodeType === Node.TEXT_NODE && obj.parentElement?.nodeType === Node.ELEMENT_NODE)

Copy link
Author

@EliasWatson EliasWatson Aug 8, 2025

Choose a reason for hiding this comment

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

Yea, I like that more. See dcbd55a

*/
export function isFunction(obj: any): obj is (...args: any[]) => any
{
if (typeof obj === 'function') return true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

When does this check fail? Why are instanceOf and subsequent fallbacks required?

Copy link
Author

@EliasWatson EliasWatson Aug 8, 2025

Choose a reason for hiding this comment

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

Good point, I don't think other checks are necessary. See dcbd55a

Comment on lines 106 to 108
return obj.nodeName === 'CANVAS'
&& typeof obj.getContext === 'function'
&& typeof obj.toDataURL === 'function';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we be exhaustive and ensure that this is first an HTMLElement with isHTMLElement, then further refine to specific element type?

Copy link
Author

Choose a reason for hiding this comment

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

That's a good idea. See dcbd55a

@thejustinwalsh
Copy link
Collaborator

thejustinwalsh commented Aug 8, 2025

Awesome thanks!
@EliasWatson To confirm this still works in your enterprise app after the latest changes?

@EliasWatson
Copy link
Author

Awesome thanks! To confirm this still works in your enterprise app after the latest changes?

Yup, I just tested it and it's still working.

@thejustinwalsh thejustinwalsh requested a review from trezy August 8, 2025 19:11
@lunarraid
Copy link
Collaborator

The only concern I have with this is this could potentially hide the fact that a project has accidentally imported more than a single version of the PixiJS library. The instanceof has caught this multiple times in my debugging third party projects.

@thejustinwalsh
Copy link
Collaborator

Ahh, good point. You technically need to encode the version information, or rely on a uuid that is unique per version in that case as well…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants