Skip to content

Wait for amp-selector to build before submitting#7190

Merged
mkhatib merged 6 commits intoampproject:masterfrom
mkhatib:build-promise
Jan 31, 2017
Merged

Wait for amp-selector to build before submitting#7190
mkhatib merged 6 commits intoampproject:masterfrom
mkhatib:build-promise

Conversation

@mkhatib
Copy link
Copy Markdown
Contributor

@mkhatib mkhatib commented Jan 25, 2017

Closes #7032
Closes #6951
Closes #6927


/**
* A list of external dependencies that can be included in forms.
* @type {Array<string>}
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.

!Array<string>

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

// Wait for an element to be built to make sure it is ready.
const depPromises = toArray(depElements).map(el => el.whenBuilt());
return Promise.race(
[Promise.all(depPromises), this.timer_.promise(500)]);
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.

500 is not enough. Let's go with at least 2s

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

this.buildPromiseRejector_ = null;

/** @private {!Promise} */
this.buildPromise_ = new Promise((resolve, reject) => {
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.

Can we make this lazy?

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.

You mean creating the promise? Yes sure.

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

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Jan 25, 2017

@dvoytenko PTAL 👀

@jridgewell
Copy link
Copy Markdown
Contributor

Closes #6951
Closes #6951

Are there supposed to be two separate issues there?

Copy link
Copy Markdown
Contributor

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

Do we need a new error state to explain why this form won't submit (when the selector failed to build)?

const depElements = this.form_.querySelectorAll(EXTERNAL_DEPS.join(','));
// Wait for an element to be built to make sure it is ready.
const depPromises = toArray(depElements).map(el => el.whenBuilt());
return Promise.race(
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.

Can we cache this, so we don't have to re-query every time?

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

if (this.buildPromise_) {
return this.buildPromise_;
}
return this.buildPromise_ = new Promise((resolve, reject) => {
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.

There's no reason to hold onto these references is we're already built.

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.

This logic is now changed - @dvoytenko implemented a signal system in #7205

return this.buildPromise_;
}
return this.buildPromise_ = new Promise((resolve, reject) => {
this.buildPromiseResolver_ = 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.

We need to know whether the build failed as well.

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.

ditto

@jridgewell
Copy link
Copy Markdown
Contributor

Let's rename the PR.

@mkhatib mkhatib changed the title Introduce whenBuilt() that gets resolved when the element is built Wait for amp-selector to build before submitting Jan 26, 2017
@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Jan 26, 2017

@dvoytenko & @jridgewell PTAL 👀

// Wait for an element to be built to make sure it is ready.
const depPromises = toArray(depElements).map(el => el.whenBuilt());
return this.dependenciesPromise_ = Promise.race(
[Promise.all(depPromises), this.timer_.promise(15000)]);
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.

15 seconds?

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.

Woops good catch I was testing this and forgot to reset it to 2000

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.

Fixed


/**
* Returns a promise that will be resolved when all dependencies used inside the form
* tag are loaded and built (e.g. amp-selector) or 500ms timeout - whichever is first.
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.

Update jsdoc.

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.

LGTM with a tiny request for doc update.

this.urlReplacement_ = urlReplacementsForDoc(element);

/** @private {?Promise} */
this.dependenciesPromise_ = null;
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.

nit(optional): i would call this - haveDepsLoadedPromise_

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.

Sticking to old name, the method name is more important as it describes the wait better.

@mkhatib mkhatib merged commit 3cc3c6a into ampproject:master Jan 31, 2017
@mkhatib mkhatib deleted the build-promise branch January 31, 2017 23:30
torch2424 pushed a commit to torch2424/amphtml that referenced this pull request Feb 14, 2017
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
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.

4 participants