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

Function getElementServiceIfAvailableForDoc

src/element-service.js:83–115  ·  view source on GitHub ↗
(
  element,
  id,
  extension,
  opt_element
)

Source from the content-addressed store, hash-verified

81 * @return {!Promise<?Object>}
82 */
83export function getElementServiceIfAvailableForDoc(
84 element,
85 id,
86 extension,
87 opt_element
88) {
89 const s = getServicePromiseOrNullForDoc(element, id);
90 if (s) {
91 return /** @type {!Promise<?Object>} */ (s);
92 }
93 const ampdoc = getAmpdoc(element);
94 return ampdoc
95 .whenExtensionsKnown()
96 .then(() => {
97 const version = ampdoc.getExtensionVersion(extension);
98 if (!version) {
99 return null;
100 }
101 const extensions = getService(ampdoc.win, 'extensions');
102 return extensions.waitForExtension(extension, version);
103 })
104 .then((ext) => {
105 if (!ext) {
106 return null;
107 }
108 // If this service is provided by an element, then we can't depend on
109 // the service (they may not use the element).
110 if (opt_element) {
111 return getServicePromiseOrNullForDoc(element, id);
112 }
113 return getServicePromiseForDoc(element, id);
114 });
115}
116
117/**
118 * Returns a promise for service for the given id in the embed scope of

Calls 8

getAmpdocFunction · 0.90
getServiceFunction · 0.90
getServicePromiseForDocFunction · 0.90
whenExtensionsKnownMethod · 0.80
waitForExtensionMethod · 0.80
thenMethod · 0.45
getExtensionVersionMethod · 0.45

Tested by

no test coverage detected