From b6d84207b00f0e17fe0e2796cc9507a5d4ec8012 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Tue, 3 Nov 2020 19:02:21 -0500 Subject: [PATCH] resources: remove LayoutDelayMeter (#30989) --- src/custom-element.js | 24 ------- src/layout-delay-meter.js | 96 --------------------------- test/unit/test-layout-delay-meter.js | 99 ---------------------------- 3 files changed, 219 deletions(-) delete mode 100644 src/layout-delay-meter.js delete mode 100644 test/unit/test-layout-delay-meter.js diff --git a/src/custom-element.js b/src/custom-element.js index 0cb4194facbd..083f865918bb 100644 --- a/src/custom-element.js +++ b/src/custom-element.js @@ -24,7 +24,6 @@ import { isInternalElement, isLoadingAllowed, } from './layout'; -import {LayoutDelayMeter} from './layout-delay-meter'; import {ResourceState} from './service/resource'; import {Services} from './services'; import {Signals} from './utils/signals'; @@ -259,9 +258,6 @@ function createBaseCustomElementClass(win) { /** @private {boolean} */ this.perfOn_ = perf && perf.isPerformanceTrackingOn(); - /** @private {?./layout-delay-meter.LayoutDelayMeter} */ - this.layoutDelayMeter_ = null; - if (nonStructThis[dom.UPGRADE_TO_CUSTOMELEMENT_RESOLVER]) { nonStructThis[dom.UPGRADE_TO_CUSTOMELEMENT_RESOLVER](nonStructThis); delete nonStructThis[dom.UPGRADE_TO_CUSTOMELEMENT_RESOLVER]; @@ -1155,9 +1151,6 @@ function createBaseCustomElementClass(win) { if (isLoadEvent) { this.signals_.signal(CommonSignals.LOAD_START); } - if (this.perfOn_) { - this.getLayoutDelayMeter_().startLayout(); - } // Potentially start the loading indicator. this.toggleLoading(true); @@ -1244,9 +1237,6 @@ function createBaseCustomElementClass(win) { updateInViewport_(inViewport) { this.implementation_.inViewport_ = inViewport; this.implementation_.viewportCallback(inViewport); - if (inViewport && this.perfOn_) { - this.getLayoutDelayMeter_().enterViewport(); - } } /** @@ -1651,20 +1641,6 @@ function createBaseCustomElementClass(win) { } } - /** - * Returns an optional overflow element for this custom element. - * @return {!./layout-delay-meter.LayoutDelayMeter} - */ - getLayoutDelayMeter_() { - if (!this.layoutDelayMeter_) { - this.layoutDelayMeter_ = new LayoutDelayMeter( - toWin(this.ownerDocument.defaultView), - this.getLayoutPriority() - ); - } - return this.layoutDelayMeter_; - } - /** * Returns an optional overflow element for this custom element. * @return {?Element} diff --git a/src/layout-delay-meter.js b/src/layout-delay-meter.js deleted file mode 100644 index 718b4c135646..000000000000 --- a/src/layout-delay-meter.js +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Copyright 2017 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import {Services} from './services'; -import {TickLabel} from './enums'; - -const LABEL_MAP = { - 0: TickLabel.CONTENT_LAYOUT_DELAY, - 2: TickLabel.ADS_LAYOUT_DELAY, -}; - -/** - * Measures the time latency between "first time in viewport" and - * "start to layout" of an element. - */ -export class LayoutDelayMeter { - /** - * @param {!Window} win - * @param {number} priority - */ - constructor(win, priority) { - /** @private {!Window} */ - this.win_ = win; - /** @private {?./service/performance-impl.Performance} */ - this.performance_ = Services.performanceForOrNull(win); - /** @private {?number} */ - this.firstInViewportTime_ = null; - /** @private {?number} */ - this.firstLayoutTime_ = null; - /** @private {boolean} */ - this.done_ = false; - /** @private {?TickLabel} */ - this.label_ = LABEL_MAP[priority]; - } - - /** - * - */ - enterViewport() { - if (!this.label_ || this.firstInViewportTime_) { - return; - } - this.firstInViewportTime_ = this.win_.Date.now(); - this.tryMeasureDelay_(); - } - - /** - * starts layout - */ - startLayout() { - if (!this.label_ || this.firstLayoutTime_) { - return; - } - this.firstLayoutTime_ = this.win_.Date.now(); - this.tryMeasureDelay_(); - } - - /** - * Tries to measure delay - */ - tryMeasureDelay_() { - if (!this.performance_ || !this.performance_.isPerformanceTrackingOn()) { - return; - } - if (this.done_) { - // Already measured. - return; - } - if (!this.firstInViewportTime_ || !this.firstLayoutTime_) { - // Not ready yet. - return; - } - const delay = this.win_.Math.max( - this.firstLayoutTime_ - this.firstInViewportTime_, - 0 - ); - if (this.label_) { - this.performance_.tickDelta(this.label_, delay); - } - this.performance_.throttledFlush(); - this.done_ = true; - } -} diff --git a/test/unit/test-layout-delay-meter.js b/test/unit/test-layout-delay-meter.js deleted file mode 100644 index a653b70b14a1..000000000000 --- a/test/unit/test-layout-delay-meter.js +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Copyright 2017 The AMP HTML Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS-IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as lolex from 'lolex'; -import {LayoutDelayMeter} from '../../src/layout-delay-meter'; -import {Services} from '../../src/services'; -import {installPerformanceService} from '../../src/service/performance-impl'; -import {installPlatformService} from '../../src/service/platform-impl'; - -describes.realWin( - 'layout-delay-meter', - { - amp: { - ampdoc: 'single', - }, - }, - (env) => { - let win; - let meter; - let tickSpy; - let clock; - - beforeEach(() => { - win = env.win; - installPlatformService(win); - installPerformanceService(win); - const perf = Services.performanceFor(win); - env.sandbox.stub(perf, 'isPerformanceTrackingOn').callsFake(() => true); - clock = lolex.install({ - target: win, - toFake: ['Date', 'setTimeout', 'clearTimeout'], - }); - tickSpy = env.sandbox.spy(perf, 'tickDelta'); - - meter = new LayoutDelayMeter(win, 2); - }); - - afterEach(() => { - clock.uninstall(); - }); - - it('should tick when there is a delay', () => { - clock.tick(100); - meter.enterViewport(); // first time in viewport - clock.tick(100); - meter.enterViewport(); // second time in viewport - clock.tick(200); - meter.startLayout(); - expect(tickSpy).to.be.calledWith('adld', 300); - - // should only tick once. - tickSpy.resetHistory(); - clock.tick(200); - meter.startLayout(); - expect(tickSpy).to.not.be.called; - }); - - it('should tick when there is no delay', () => { - clock.tick(100); - meter.startLayout(); - clock.tick(200); - meter.enterViewport(); - expect(tickSpy).to.be.calledWith('adld', 0); - - // should only tick once. - tickSpy.resetHistory(); - clock.tick(200); - meter.enterViewport(); - expect(tickSpy).to.not.be.called; - }); - - it('should not tick if it never enterViewport', () => { - clock.tick(100); - meter.startLayout(); - clock.tick(200); - expect(tickSpy).not.to.be.called; - }); - - it('should not tick if it never startLayout', () => { - clock.tick(100); - meter.enterViewport(); - clock.tick(200); - expect(tickSpy).not.to.be.called; - }); - } -);