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

Unify CacheState tracking for both legacy and remote video caching systems. #35252

Merged
merged 2 commits into from Jul 16, 2021

Conversation

gmajoulet
Copy link
Contributor

Unify CacheState tracking for both legacy and remote video caching systems.

@amp-owners-bot
Copy link

Hey @newmuis! These files were changed:

extensions/amp-story/1.0/media-performance-metrics-service.js
extensions/amp-story/1.0/test/test-media-performance-metrics-service.js

Copy link
Contributor

@mszylkowski mszylkowski left a comment

Choose a reason for hiding this comment

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

Left nits for code readability

Comment on lines 435 to 440
if (media.currentSrc === source.src && isCachedSource(source)) {
return CacheState.CACHE;
}
if (isCachedSource(source)) {
hasCachedSource = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: To prevent defining the anon function, you could do

// All video caching mechanisms rely on HTMLSourceElements and never a src
// on the HTMLMediaElement as it does not allow for fallback sources.
const sources = toArray(media.querySelectorAll('source'));

let hasCachedSource = false;
for (const source of sources) {
  if (source.hasAttribute('i-amphtml-video-cached-source')) {
    // You can explain why this returns early so we understand when we look back at it (either this or the method documentation)
    if (media.currentSrc === source.src) {
      return CacheState.CACHE;
    }
    hasCachedSource = true;
  }
}
return hasCachedSource ? CacheState.ORIGIN_CACHE_MISS : CacheState.ORIGIN;

Also, keeping the declaration of hasCachedSource next to the loop will make it a bit easier to follow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to kindly push back on the nested condition :( I agree that removing the anon function is cool, but my brain is really bad at following nested conditions or else statements (i.e. conditional conditions). This is why I always keep only one level of indentation, I found it to yield much much much easier to maintain code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still removed the anon function, thanks for the suggestion

@gmajoulet gmajoulet enabled auto-merge (squash) July 16, 2021 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants