MCPcopy
hub / github.com/ampproject/amphtml / coreServicesAvailable

Method coreServicesAvailable

src/service/performance-impl.js:293–365  ·  view source on GitHub ↗

* Listens to viewer and resource events. * @return {!Promise}

()

Source from the content-addressed store, hash-verified

291 * @return {!Promise}
292 */
293 coreServicesAvailable() {
294 const {documentElement} = this.win.document;
295 this.ampdoc_ = Services.ampdoc(documentElement);
296 this.viewer_ = Services.viewerForDoc(documentElement);
297 this.resources_ = Services.resourcesForDoc(documentElement);
298 this.documentInfo_ = Services.documentInfoForDoc(this.ampdoc_);
299
300 this.isPerformanceTrackingOn_ =
301 this.viewer_.isEmbedded() && this.viewer_.getParam('csi') === '1';
302
303 // This is for redundancy. Call flush on any visibility change.
304 this.ampdoc_.onVisibilityChanged(this.flush.bind(this));
305
306 // Does not need to wait for messaging ready since it will be queued
307 // if it isn't ready.
308 this.measureUserPerceivedVisualCompletenessTime_();
309
310 // Can be null which would mean this AMP page is not embedded
311 // and has no messaging channel.
312 const channelPromise = this.viewer_.whenMessagingReady();
313
314 this.ampdoc_.whenFirstVisible().then(() => {
315 this.tick(TickLabel_Enum.ON_FIRST_VISIBLE);
316 this.flush();
317 });
318
319 const registerVisibilityChangeListener =
320 this.supportsLargestContentfulPaint_ || this.supportsLayoutShift_;
321 // Register a handler to record metrics when the page enters the hidden
322 // lifecycle state.
323 if (registerVisibilityChangeListener) {
324 this.ampdoc_.onVisibilityChanged(this.onAmpDocVisibilityChange_);
325 }
326
327 // We don't check `isPerformanceTrackingOn` here since there are some
328 // events that we call on the viewer even though performance tracking
329 // is off we only need to know if the AMP page has a messaging
330 // channel or not.
331 if (!channelPromise) {
332 return Promise.resolve();
333 }
334
335 return channelPromise
336 .then(() => {
337 // Tick the "messaging ready" signal.
338 this.tickDelta(
339 TickLabel_Enum.MESSAGING_READY,
340 this.win.performance.now()
341 );
342
343 // Tick timeOrigin so that epoch time can be calculated by consumers.
344 this.tick(TickLabel_Enum.TIME_ORIGIN, undefined, this.timeOrigin_);
345
346 const usqp = this.ampdoc_.getMetaByName('amp-usqp');
347 if (usqp) {
348 usqp.split(',').forEach((exp) => {
349 this.addEnabledExperiment('ssr-' + exp);
350 });

Callers 3

bootstrapFunction · 0.80
amp-inabox.jsFile · 0.80

Calls 15

tickMethod · 0.95
flushMethod · 0.95
tickDeltaMethod · 0.95
addEnabledExperimentMethod · 0.95
flushQueuedTicks_Method · 0.95
resolveMethod · 0.80
nowMethod · 0.80
getMetaByNameMethod · 0.80
isEmbeddedMethod · 0.45
getParamMethod · 0.45

Tested by

no test coverage detected