-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix duplicate p5.js init by checking window.p5 and skipping if alread… #7791
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
base: dev-2.0
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The recommendation is to just check whether window.p5
is already defined or not. It doesn't matter whether that p5
variable comes from previously loaded p5.js or not as there's no easy way to check it.
src/core/main.js
Outdated
if (window.p5 instanceof p5) { | ||
console.warn('p5.js appears to have been imported multiple times. This could cause errors.'); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
window.p5
is the constructor so it won't be an instance of p5
, this condition will always be false.
src/core/main.js
Outdated
this._isGlobal = true; | ||
window.p5 = this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overwriting window.p5
here will break a lot of things, it shouldn't be redefined.
52bd064
to
717784f
Compare
Hi @limzykenneth,Sorry for the late response—I’ve been busy with my semester exams. I made some changes to the code based on the previous discussion. Could you please take a look at it? |
Now that I think about it, I'm not sure the idea I had would work as in this case p5 would just detect itself as having already been defined as |
@limzykenneth Yes, I did test the code, but unfortunately didn’t get the desired result. I think your concern about p5 being already attached to window.p5 and causing initialization issues might actually be what's happening here. I’m still experimenting to better understand the behavior |
…y loaded
Resolves #7687
Changes:
window.p5 instanceof p5
before global bindingObject.defineProperty
redefinitionScreenshots of the change:
PR Checklist
npm run lint
passes