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

Method attachShadowDocAsStream

src/multidoc-manager.js:261–308  ·  view source on GitHub ↗

* Implementation for `attachShadowDocAsStream` function. Attaches the shadow * doc and configures ampdoc for it. * @param {!Element} hostElement * @param {string} url * @param {!{[key: string]: string}=} opt_initParams * @return {!Object}

(hostElement, url, opt_initParams)

Source from the content-addressed store, hash-verified

259 * @return {!Object}
260 */
261 attachShadowDocAsStream(hostElement, url, opt_initParams) {
262 user().assertString(url);
263 dev().fine(TAG, 'Attach shadow doc as stream');
264 return this.attachShadowDoc_(
265 hostElement,
266 url,
267 opt_initParams,
268 (amp, shadowRoot, ampdoc) => {
269 // Start streaming.
270 let renderStarted = false;
271 const writer = createShadowDomWriter(this.win);
272 amp['writer'] = writer;
273 writer.onBody((doc) => {
274 // Install extensions.
275 this.mergeShadowHead_(ampdoc, shadowRoot, doc);
276
277 // Append shallow body.
278 const body = importShadowBody(
279 shadowRoot,
280 dev().assertElement(doc.body),
281 /* deep */ false
282 );
283 body.classList.add('amp-shadow');
284 ampdoc.setBody(body);
285 return body;
286 });
287 writer.onBodyChunk(() => {
288 // TODO(dvoytenko): find a better and more stable way to make
289 // content visible. E.g. integrate with dynamic classes. In shadow
290 // case specifically, we have to wait for stubbing to complete,
291 // which may take awhile due to node importing.
292 if (!renderStarted) {
293 renderStarted = true;
294 setTimeout(() => {
295 ampdoc.signals().signal(CommonSignals_Enum.RENDER_START);
296 setStyle(hostElement, 'visibility', 'visible');
297 }, 50);
298 }
299 });
300 return new Promise((resolve) => {
301 writer.onEnd(() => {
302 resolve();
303 amp.writer = null;
304 });
305 });
306 }
307 );
308 }
309
310 /**
311 * Processes the contents of the shadow document's head.

Callers 2

test-runtime.jsFile · 0.80
showAsStreamMethod · 0.80

Calls 15

attachShadowDoc_Method · 0.95
mergeShadowHead_Method · 0.95
userFunction · 0.90
devFunction · 0.90
createShadowDomWriterFunction · 0.90
importShadowBodyFunction · 0.90
setStyleFunction · 0.90
assertStringMethod · 0.80
fineMethod · 0.80
assertElementMethod · 0.80
setBodyMethod · 0.80
resolveFunction · 0.50

Tested by

no test coverage detected