MCPcopy Index your code
hub / github.com/ampproject/amphtml / startLayout

Method startLayout

src/service/resource.js:841–905  ·  view source on GitHub ↗

* Starts the layout of the resource. Returns the promise that will yield * once layout is complete. Only allowed to be called on a upgraded, built * and displayed element. * @return {!Promise}

()

Source from the content-addressed store, hash-verified

839 * @return {!Promise}
840 */
841 startLayout() {
842 if (this.layoutPromise_) {
843 return this.layoutPromise_;
844 }
845 if (this.state_ == ResourceState_Enum.LAYOUT_COMPLETE) {
846 return Promise.resolve();
847 }
848 if (this.state_ == ResourceState_Enum.LAYOUT_FAILED) {
849 return Promise.reject(this.lastLayoutError_);
850 }
851
852 devAssert(
853 this.state_ != ResourceState_Enum.NOT_BUILT,
854 'Not ready to start layout: %s (%s)',
855 this.debugid,
856 this.state_
857 );
858 devAssert(this.isDisplayed(), 'Not displayed for layout: %s', this.debugid);
859
860 if (this.state_ != ResourceState_Enum.LAYOUT_SCHEDULED) {
861 const err = dev().createExpectedError(
862 'startLayout called but not LAYOUT_SCHEDULED',
863 'currently: ',
864 this.state_
865 );
866 reportError(err, this.element);
867 return Promise.reject(err);
868 }
869
870 // Unwanted re-layouts are ignored.
871 if (this.layoutCount_ > 0 && !this.element.isRelayoutNeeded()) {
872 dev().fine(
873 TAG,
874 "layout canceled since it wasn't requested:",
875 this.debugid,
876 this.state_
877 );
878 this.state_ = ResourceState_Enum.LAYOUT_COMPLETE;
879 return Promise.resolve();
880 }
881
882 dev().fine(TAG, 'start layout:', this.debugid, 'count:', this.layoutCount_);
883 this.layoutCount_++;
884 this.state_ = ResourceState_Enum.LAYOUT_SCHEDULED;
885 this.abortController_ = new AbortController();
886 const {signal} = this.abortController_;
887
888 const promise = new Promise((resolve, reject) => {
889 Services.vsyncFor(this.hostWin).mutate(() => {
890 let callbackResult;
891 try {
892 callbackResult = this.element.layoutCallback(signal);
893 } catch (e) {
894 reject(e);
895 }
896 Promise.resolve(callbackResult).then(resolve, reject);
897 });
898 signal.onabort = () => reject(cancellation());

Callers 3

test-resource.jsFile · 0.80
doPass_Method · 0.80

Calls 13

isDisplayedMethod · 0.95
layoutComplete_Method · 0.95
devAssertFunction · 0.90
devFunction · 0.90
reportErrorFunction · 0.90
cancellationFunction · 0.90
resolveMethod · 0.80
fineMethod · 0.80
createExpectedErrorMethod · 0.45
isRelayoutNeededMethod · 0.45
thenMethod · 0.45
mutateMethod · 0.45

Tested by

no test coverage detected