-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Workspace: InOb-based load scheduler #31591
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<!doctype html> | ||
<html ⚡> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Lorem Ipsum | PublisherName</title> | ||
<link rel="canonical" href="https://medium.com/p/cb7f223fad86" > | ||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
|
||
<style amp-custom> | ||
body { | ||
margin: 0; | ||
font-family: 'Georgia', Serif; | ||
background: white; | ||
} | ||
|
||
</style> | ||
<link href='https://fonts.googleapis.com/css?family=Georgia|Open+Sans|Roboto' rel='stylesheet' type='text/css'> | ||
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript> | ||
</head> | ||
<body> | ||
<div style="height: 0vh">scroll</div> | ||
|
||
<amp-img id="hero-img" | ||
src="img/[email protected]" | ||
srcset="img/[email protected] 1x, img/[email protected] 2x" | ||
layout="responsive" width="360" | ||
height="216"> | ||
</amp-img> | ||
|
||
<amp-img id="ad-img" | ||
src="img/[email protected]" | ||
layout="responsive" width="360" | ||
height="216" | ||
xpri="2" | ||
noprerender> | ||
</amp-img> | ||
|
||
<div style="height: 30vh">scroll</div> | ||
|
||
<amp-img id="offscreen-img" | ||
src="img/clock.jpg" | ||
layout="responsive" width="360" | ||
height="216"> | ||
</amp-img> | ||
|
||
<div style="height: 2000vh">scroll</div> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,8 @@ const UpgradeState = { | |
UPGRADE_IN_PROGRESS: 4, | ||
}; | ||
|
||
const EMPTY_FUNC = () => {}; | ||
|
||
/** | ||
* Caches whether the template tag is supported to avoid memory allocations. | ||
* @type {boolean|undefined} | ||
|
@@ -340,6 +342,17 @@ function createBaseCustomElementClass(win) { | |
return this.upgradeDelayMs_; | ||
} | ||
|
||
/** | ||
* Whether this element supports loaderV2 scheduler. | ||
* | ||
* @return {boolean} | ||
* @final | ||
* TODO(#31915): rename to just isLoadable | ||
*/ | ||
isLoadableV2() { | ||
return this.implementation_.isLoadableV2(); | ||
} | ||
|
||
/** | ||
* Completes the upgrade of the element with the provided implementation. | ||
* @param {!./base-element.BaseElement} newImpl | ||
|
@@ -396,6 +409,15 @@ function createBaseCustomElementClass(win) { | |
return this.signals_.whenSignal(CommonSignals.BUILT); | ||
} | ||
|
||
/** | ||
* Returns the promise that's rewsolved when the element has been loaded. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rewsolved --> resolved |
||
* If the loading fails, the resulting promise is rejected. | ||
* @return {!Promise} | ||
*/ | ||
whenLoaded() { | ||
return this.signals_.whenSignal(CommonSignals.LOAD_END).then(EMPTY_FUNC); | ||
} | ||
|
||
/** | ||
* Get the priority to load the element. | ||
* @return {number} | ||
|
@@ -497,6 +519,14 @@ function createBaseCustomElementClass(win) { | |
)); | ||
} | ||
|
||
/** | ||
* Forces immediate load of this element. | ||
* @return {!Promise} | ||
*/ | ||
load() { | ||
return this.build().then(() => this.getResources().forceLoad(this)); | ||
} | ||
|
||
/** | ||
* Called to instruct the element to preconnect to hosts it uses during | ||
* layout. | ||
|
@@ -534,12 +564,10 @@ function createBaseCustomElementClass(win) { | |
/** | ||
* Updates the layout box of the element. | ||
* Should only be called by Resources. | ||
* @param {!./layout-rect.LayoutRectDef} layoutBox | ||
* @param {boolean} sizeChanged | ||
*/ | ||
updateLayoutBox(layoutBox, sizeChanged = false) { | ||
updateLayoutBox() { | ||
if (this.isBuilt()) { | ||
this.onMeasure(sizeChanged); | ||
this.onMeasure(); | ||
} | ||
} | ||
|
||
|
@@ -877,6 +905,9 @@ function createBaseCustomElementClass(win) { | |
/** @private */ | ||
connected_() { | ||
if (this.built_) { | ||
if (this.isLoadableV2()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't one of the big points here that we don't need to branch on |
||
this.getResources().scheduleLoad(this); | ||
} | ||
this.implementation_.attachedCallback(); | ||
} | ||
} | ||
|
@@ -1112,11 +1143,8 @@ function createBaseCustomElementClass(win) { | |
} | ||
|
||
this.dispatchCustomEventForTesting(AmpEvents.LOAD_START); | ||
const isLoadEvent = this.layoutCount_ == 0; // First layout is "load". | ||
this.signals_.reset(CommonSignals.UNLOAD); | ||
if (isLoadEvent) { | ||
this.signals_.signal(CommonSignals.LOAD_START); | ||
} | ||
this.signals_.signal(CommonSignals.LOAD_START); | ||
|
||
// Potentially start the loading indicator. | ||
this.toggleLoading(true); | ||
|
@@ -1130,9 +1158,10 @@ function createBaseCustomElementClass(win) { | |
if ((!getMode().test || signal) && signal.aborted) { | ||
throw cancellation(); | ||
} | ||
if (isLoadEvent) { | ||
this.signals_.signal(CommonSignals.LOAD_END); | ||
if (this.isLoadableV2()) { | ||
this.getResources().unscheduleLoad(this); | ||
} | ||
this.signals_.signal(CommonSignals.LOAD_END); | ||
this.readyState = 'complete'; | ||
this.layoutCount_++; | ||
this.toggleLoading(false); | ||
|
@@ -1148,13 +1177,14 @@ function createBaseCustomElementClass(win) { | |
if ((!getMode().test || signal) && signal.aborted) { | ||
throw cancellation(); | ||
} | ||
// add layoutCount_ by 1 despite load fails or not | ||
if (isLoadEvent) { | ||
this.signals_.rejectSignal( | ||
CommonSignals.LOAD_END, | ||
/** @type {!Error} */ (reason) | ||
); | ||
if (this.isLoadableV2()) { | ||
this.getResources().unscheduleLoad(this); | ||
} | ||
// add layoutCount_ by 1 despite load fails or not | ||
this.signals_.rejectSignal( | ||
CommonSignals.LOAD_END, | ||
/** @type {!Error} */ (reason) | ||
); | ||
this.layoutCount_++; | ||
this.toggleLoading(false); | ||
throw reason; | ||
|
@@ -1226,6 +1256,9 @@ function createBaseCustomElementClass(win) { | |
const isReLayoutNeeded = this.implementation_.unlayoutCallback(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: isReLayoutNeeded --> isRelayoutNeeded |
||
if (isReLayoutNeeded) { | ||
this.reset_(); | ||
if (this.isLoadableV2()) { | ||
this.getResources().scheduleLoad(this); | ||
} | ||
} | ||
this.dispatchCustomEventForTesting(AmpEvents.UNLOAD); | ||
return isReLayoutNeeded; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that this (and build/layout) should not have
callback
in their names. I gave the full reasoning here #31046 (comment). The tldr is that we are asking an element to load as opposed to informing it that load has happened.I understand the desire to match CE's naming scheme, but IMO it doesn't make sense for these methods