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

Cleanup of P5Util class #1319

Merged
merged 2 commits into from
Mar 15, 2022
Merged

Cleanup of P5Util class #1319

merged 2 commits into from
Mar 15, 2022

Conversation

lindapaiste
Copy link
Contributor

  • Improved JSDoc types.
  • Reject the promises in getBlob and loadAsync when the underlying functions fail.
  • Renamed arguments x and y to width and height for clarity in method rawToBlob.
  • Inlined redundant local variables.

This PR extends the commit in #1318 to avoid creating conflicts in the P5Util constructor.

@joeyklee joeyklee added the SEMVER/patch a version tag for a patch release change label Mar 7, 2022
// Ensure the p5Color object is an RGB array
Object.keys(result).forEach(part => {
if (result[part].color instanceof window.p5.Color) {
if (result[part].color instanceof p5.Color) {
Copy link
Contributor

Choose a reason for hiding this comment

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

positive affirmation:

  • nice one! 🥇

@@ -5,21 +5,31 @@

class P5Util {
constructor() {
if (typeof window !== "undefined") {
Copy link
Contributor

Choose a reason for hiding this comment

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

positive affirmation:

  • huzzah! 🙌

this.p5Instance.loadImage(url, (img) => {
resolve(img);
}, () => {
reject(new Error(`Could not load image from url ${url}`));
Copy link
Contributor

Choose a reason for hiding this comment

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

positive affirmation:

  • Yesss! Thanks so much for digging into the error handling here <3 🏅

* @param {number} height
* @returns {Promise<Blob>}
*/
async rawToBlob(raws, width, height) {
Copy link
Contributor

Choose a reason for hiding this comment

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

positive affirmation:

  • this is so helpful and makes much more sense to use width & height 🙌

Copy link
Contributor

@joeyklee joeyklee left a comment

Choose a reason for hiding this comment

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

LGTM! Again, I'm loving the clarity you're bringing into both the code (jsdoc updates will help so much w/ introducing Types) and downstream end-user experience (e.g. adding error handling!). I'm excited about how these changes are moving us closer to being able to support ml5 in a node context! 🚀

Copy link

@proxycase proxycase left a comment

Choose a reason for hiding this comment

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

These changes look really helpful! I just had a question.

if (this.checkP5()) {
const p5Img = await this.loadAsync(URL.createObjectURL(blob));
return p5Img;
if (this.checkP5() && typeof URL !== "undefined") {

Choose a reason for hiding this comment

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

Hello! I'm just reviewing code for fun. Curious – can you not just say if (this.checkP5() && URL) {...? Is it necessary to use typeof in order to check that it's not undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great question! That particular check is part of a larger effort towards making sure that ml5 can run in a backend Node.js environment without error. Here I am making sure that the global URL constructor exists before I try to use it.

If a variable such as URL is never declared anywhere (with let, const, var, class, etc.) then trying to access if (URL) is a fatal ReferenceError. But checking typeof URL !== "undefined" is ok and lets you know that it's safe to use that variable. We definitely have to do this when accessing window.

It might be that's it's not actually needed for URL because Node does have its own URL class, which I didn't know until right now 😄

Choose a reason for hiding this comment

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

Interesting! That makes sense to me. Appreciate the response – I've learned something new!

@joeyklee joeyklee merged commit 5339fa4 into ml5js:main Mar 15, 2022
@lindapaiste lindapaiste deleted the cleanup/p5utils branch May 9, 2022 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SEMVER/patch a version tag for a patch release change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants