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

core(start-url): reorganize start_url gatherer #4838

Merged

Conversation

paulirish
Copy link
Member

(This is a merge into #4710)

This is reusing the exact same new logic from 4710, just separated a bit.

Only interesting things:

  1. using isReadFailure boolean to indicate if we should bail and fail before trying a fetch
  2. added a timeout to make sure we're not waiting forever for a matching network request
  3. more comments & jsdoc

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

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

seems fine to me though that async/await 💊 is what this file really needs IMO :)

// Wait up to 3s to get a matched network request from the fetch() to work
const timeoutPromise = new Promise(resolve => setTimeout(_ =>
resolve({statusCode: -1, debugString: 'Timed out waiting for fetched start_url'})
, 3000));
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is some strange indentation haha is this prettier's handiwork?

Copy link
Collaborator

Choose a reason for hiding this comment

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

probably, was thinking the same but didn't dare to comment 😛

driver.on('Network.responseReceived', onResponseReceived);

// Deliberately not returning, as _attemptManifestFetch resolves when we have a matched network request
driver.evaluateAsync(`fetch('${startUrl}')`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we want to have a catch though to prevent unhandled promise rejection?

Copy link
Member

Choose a reason for hiding this comment

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

can this be pulled out and do

return driver.evaluateAsync(`fetch('${startUrl}')`)
  .then(_ => Promise.race([fetchPromise, timeoutPromise]));

at the end?

Copy link
Member Author

Choose a reason for hiding this comment

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

do we want to have a catch though to prevent unhandled promise rejection?

yes.. like just adding: .catch(reject).

can this be pulled out ...

hm. it's not that i think the fetch() may take forever but that the onResponseRecieved fn may not resolve

Copy link
Member Author

Choose a reason for hiding this comment

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

can this be pulled out and do

goooooooood calllllll. sorry didnt understand. yah this is good.

/**
* Read the parsed manifest and return failure reasons or the startUrl
* @param {Manifest} manifest
* @return {{isReadFailure: !boolean, reason: ?string, startUrl: ?startUrl}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't need any of the ! anymore 🎉 @brendankenny are we going with closure or ts style ??

Copy link
Member

Choose a reason for hiding this comment

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

no more !! Assume everything is non-nullable and add ? (or |null) if you really want something to be nullable

Copy link
Member

Choose a reason for hiding this comment

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

also I think you want reason?: string, startUrl?: startUrl here since they're optional and not null? If you want to get real fancy you can do

@return {{isReadFailure: true, reason: string}|{isReadFailure: false, startUrl: string}}

Copy link
Collaborator

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

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

Looks good to me

*/
_attemptManifestFetch(options, startUrl) {
// Wait up to 3s to get a matched network request from the fetch() to work
const timeoutPromise = new Promise(resolve => setTimeout(_ =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

why setTimeout(_ => and not setTimeout(() => not sure if we have a rule on this one 🙄

Copy link
Member

Choose a reason for hiding this comment

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

we've historically allowed a _ as a short param name that's always ignored, but with tsc type checking it's flagged as an implicit any typed variable if the compiler can't figure out what it is, at which point it's usually easier just to switch to (). I'm not sure if there's any way to get typescript to ignore those cases, so we may end up just ditching it. Not sure yet :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

oo, oo ditch it! ditch it! ditch it! 😃

, 3000));

const fetchPromise = new Promise(resolve => {
const driver = options.driver;
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 do ´attemptManifestFetch({driver}, startUrl) {´ ?
than we could nuke this line

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea! much better.

*/
_attemptManifestFetch(options, startUrl) {
// Wait up to 3s to get a matched network request from the fetch() to work
const timeoutPromise = new Promise(resolve => setTimeout(_ =>
Copy link
Member

Choose a reason for hiding this comment

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

do we want to hold off on adding this and just have a general "add generalized timeout method to driver commands" issue? Only bring this up because we've never had reports of this timing out, so not sure if it's worth adding in various implementations of timeouts spread out over a few different gatherers

Copy link
Member Author

Choose a reason for hiding this comment

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

i'm ok with delaying.

up till now the starturl gatherer uses fetch('${url}').then(response => response.status). That's pretty direct and I wouldn't expect it to hang forever.

Now we're fetching and observing all network requests until we see one with a matching url. it's different and i'm not 100% we'll always have a match. redirects shouldn't time out but ... it probably will mess up.

@paulirish paulirish merged commit 146812f into GoogleChrome:bug/start-url-cached Mar 21, 2018
@paulirish paulirish deleted the starturl-sparkle branch March 21, 2018 23:45
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.

5 participants