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

Method initMessagingChannel_

src/service/viewer-impl.js:280–317  ·  view source on GitHub ↗

* Initialize messaging channel with Viewer host. * This promise will resolve when communications channel has been * established or timeout in 20 seconds. The timeout is needed to avoid * this promise becoming a memory leak with accumulating undelivered * messages. The promise is only ava

(messagingPromise)

Source from the content-addressed store, hash-verified

278 * @private
279 */
280 initMessagingChannel_(messagingPromise) {
281 const isEmbedded = !!(
282 (this.isIframed_ &&
283 !this.win.__AMP_TEST_IFRAME &&
284 // Checking param "origin", as we expect all viewers to provide it.
285 // See https://github.com/ampproject/amphtml/issues/4183
286 // There appears to be a bug under investigation where the
287 // origin is sometimes failed to be detected. Since failure mode
288 // if we fail to initialize communication is very bad, we also check
289 // for visibilityState.
290 // After https://github.com/ampproject/amphtml/issues/6070
291 // is fixed we should probably only keep the amp_js_v check here.
292 (this.ampdoc.getParam('origin') ||
293 this.ampdoc.getParam('visibilityState') ||
294 // Parent asked for viewer JS. We must be embedded.
295 this.win.location.search.indexOf('amp_js_v') != -1)) ||
296 this.isWebviewEmbedded() ||
297 this.isCctEmbedded() ||
298 !this.ampdoc.isSingleDoc()
299 );
300
301 if (!isEmbedded) {
302 return null;
303 }
304 const timeoutMessage = 'initMessagingChannel timeout';
305 return Services.timerFor(this.win)
306 .timeoutPromise(20000, messagingPromise, timeoutMessage)
307 .catch((reason) => {
308 let error = getChannelError(
309 /** @type {!Error|string|undefined} */ (reason)
310 );
311 if (error && endsWith(error.message, timeoutMessage)) {
312 error = dev().createExpectedError(error);
313 }
314 reportError(error);
315 throw error;
316 });
317 }
318
319 /** @override */
320 getAmpDoc() {

Callers 1

constructorMethod · 0.95

Calls 11

isWebviewEmbeddedMethod · 0.95
isCctEmbeddedMethod · 0.95
endsWithFunction · 0.90
devFunction · 0.90
reportErrorFunction · 0.90
getChannelErrorFunction · 0.85
catchMethod · 0.80
timeoutPromiseMethod · 0.80
getParamMethod · 0.45
isSingleDocMethod · 0.45
createExpectedErrorMethod · 0.45

Tested by

no test coverage detected