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

amp-bind: Delay init until viewer first visible #9033

Merged
merged 3 commits into from Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 24 additions & 17 deletions extensions/amp-bind/0.1/amp-state.js
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {bindForDoc} from '../../../src/services';
import {bindForDoc, viewerForDoc} from '../../../src/services';
import {fetchBatchedJsonFor} from '../../../src/batched-json';
import {isBindEnabledFor} from './bind-impl';
import {isJsonScriptTag} from '../../../src/dom';
Expand Down Expand Up @@ -52,11 +52,32 @@ export class AmpState extends AMP.BaseElement {
user().assert(isBindEnabledFor(this.win),
`Experiment "amp-bind" is disabled.`);

const TAG = this.getName_();

toggle(this.element, /* opt_display */ false);
this.element.setAttribute('aria-hidden', 'true');

// Don't parse or fetch in prerender mode.
const viewer = viewerForDoc(this.getAmpDoc());
viewer.whenFirstVisible().then(() => this.initialize_());
}

/** @override */
mutatedAttributesCallback(mutations) {
const src = mutations['src'];
if (src !== undefined) {
this.fetchSrcAndUpdateState_(/* isInit */ false);
}
}

/** @override */
renderOutsideViewport() {
// We want the state data to be available wherever it is in the document.
return true;
}

/** @private */
initialize_() {
const TAG = this.getName_();

// Fetch JSON from endpoint at `src` attribute if it exists,
// otherwise parse child script tag.
if (this.element.hasAttribute('src')) {
Expand All @@ -83,20 +104,6 @@ export class AmpState extends AMP.BaseElement {
}
}

/** @override */
mutatedAttributesCallback(mutations) {
const src = mutations['src'];
if (src !== undefined) {
this.fetchSrcAndUpdateState_(/* isInit */ false);
}
}

/** @override */
renderOutsideViewport() {
// We want the state data to be available wherever it is in the document.
return true;
}

/**
* @param {boolean} isInit
* @private
Expand Down
16 changes: 12 additions & 4 deletions extensions/amp-bind/0.1/bind-impl.js
Expand Up @@ -27,7 +27,11 @@ import {isExperimentOn} from '../../../src/experiments';
import {invokeWebWorker} from '../../../src/web-worker/amp-worker';
import {isFiniteNumber} from '../../../src/types';
import {reportError} from '../../../src/error';
import {ampFormServiceForDoc, resourcesForDoc} from '../../../src/services';
import {
ampFormServiceForDoc,
resourcesForDoc,
viewerForDoc,
} from '../../../src/services';
import {filterSplice} from '../../../src/utils/array';
import {rewriteAttributeValue} from '../../../src/sanitizer';

Expand Down Expand Up @@ -124,13 +128,17 @@ export class Bind {
this.evaluator_ = new BindEvaluator();
}

/** @const @private {!../../../src/service/viewer-impl.Viewer} */
this.viewer_ = viewerForDoc(this.ampdoc);

/**
* Resolved when the service is fully initialized.
* @const @private {Promise}
*/
this.initializePromise_ = this.ampdoc.whenReady().then(() => {
return this.initialize_();
});
this.initializePromise_ = Promise.all([
this.ampdoc.whenReady(),
this.viewer_.whenFirstVisible(), // Don't initialize in prerender mode.
]).then(() => this.initialize_());

/**
* @private {?Promise}
Expand Down