Skip to content

Wait for document ready before search for element in #getElement#9486

Merged
zhouyx merged 5 commits intoampproject:masterfrom
zhouyx:wait-for-doc-ready
May 25, 2017
Merged

Wait for document ready before search for element in #getElement#9486
zhouyx merged 5 commits intoampproject:masterfrom
zhouyx:wait-for-doc-ready

Conversation

@zhouyx
Copy link
Copy Markdown
Contributor

@zhouyx zhouyx commented May 22, 2017

fix #9370

@zhouyx zhouyx requested a review from dvoytenko May 22, 2017 23:38
// And no need to wait for document ready.
if (selector == ':root') {
return this.getRootElement();
return Promise.resolve().then(() => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the tick necessary here? Or can you just immediately resolve?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need. Just return Promise.resolve(...)

Copy link
Copy Markdown
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

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

Some notes. But overall I think this is a lot better than what it was before!

* @param {?string=} selectionMethod Allowed values are `null`,
* `'closest'` and `'scope'`.
* @return {?Element} Element corresponding to the selector if found.
* @return {Promise<?Element>} Element corresponding to the selector if found.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It'll be {!Promise<?Element>}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

// And no need to wait for document ready.
if (selector == ':root') {
return this.getRootElement();
return Promise.resolve().then(() => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need. Just return Promise.resolve(...)

}
if (selector == ':host') {
return this.getHostElement();
return Promise.resolve().then(() => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto, return Promise.resolve(this.getHostElement())

Copy link
Copy Markdown
Contributor Author

@zhouyx zhouyx May 24, 2017

Choose a reason for hiding this comment

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

I used promise.catch(error) in testing.
However when I use return Promise.resolve(user.assertElement(this.getHostElement())), I was not able to catch the error. Then I changed to Promise.resolve().then(() => return user.assertElement(this.getHostElement())) and then was able to catch the error. Can someone explain why this is the case? And how should I fix it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see. Slightly weird. But I agree - it's probably better to yield error consistently.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the assertElement is throwing, then put it in a Promise constructor:

return new Promise(res => {
  resolve(user().assertElement(this.getRootElement()));
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed as suggested : )

* @param {?string=} selectionMethod Allowed values are `null`,
* `'closest'` and `'scope'`.
* @return {?AmpElement} AMP element corresponding to the selector if found.
* @return {Promise<?AmpElement>} AMP element corresponding to the selector if found.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

!Promise

}

return this.observers_[eventType].add(event => {
//console.log('add listener');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oops.

//console.log('add listener');
// Wait for target selected
targetReady.then(target => {
//console.log('trigger target handler');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove

target = user().assertElement(
element, `Element "${selector}" not found`);;
return this.getElementSignal(
'ini-load', /** @type{!Element} */ (element));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pass target instead.

selector,
selectionMethod
).then(element => {
element =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it worth adding getRequiredAmpElement and getRequiredElement() APIs in the root? They seem to come up a lot. Or we can even modify the existing APIs if this is ALWAYS the case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I checked we always check if element is not null. Hence I moved to existing APIs. PTAL

@zhouyx zhouyx force-pushed the wait-for-doc-ready branch from 6ebf9c8 to ba9ea8f Compare May 25, 2017 20:53
@zhouyx zhouyx merged commit b4519ac into ampproject:master May 25, 2017
@zhouyx zhouyx deleted the wait-for-doc-ready branch May 25, 2017 21:58
@ghost ghost mentioned this pull request Aug 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analytics root: wait for document ready before select element

5 participants