Skip to content
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

Ensure that aborting layout via unload cancels the layout promise #32099

Merged
merged 4 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build-system/test-configs/dep-check-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ exports.rules = [
'src/polyfills.js->src/polyfills/map-set.js',
'src/polyfills.js->src/polyfills/set-add.js',
'src/polyfills.js->src/polyfills/weakmap-set.js',
'src/friendly-iframe-embed.js->src/polyfills/abort-controller.js',
'src/friendly-iframe-embed.js->src/polyfills/custom-elements.js',
'src/friendly-iframe-embed.js->src/polyfills/document-contains.js',
'src/friendly-iframe-embed.js->src/polyfills/domtokenlist.js',
Expand Down
2 changes: 2 additions & 0 deletions src/friendly-iframe-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from './service';
import {escapeHtml} from './dom';
import {getExperimentBranch} from './experiments';
import {install as installAbortController} from './polyfills/abort-controller';
import {installAmpdocServicesForEmbed} from './service/core-services';
import {install as installCustomElements} from './polyfills/custom-elements';
import {install as installDOMTokenList} from './polyfills/domtokenlist';
Expand Down Expand Up @@ -715,6 +716,7 @@ function installPolyfillsInChildWindow(parentWin, childWin) {
installCustomElements(childWin, class {});
installIntersectionObserver(parentWin, childWin);
installResizeObserver(parentWin, childWin);
installAbortController(childWin);
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/polyfills/abort-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ class AbortController {

/** */
abort() {
if (this.signal_.isAborted_) {
// Already aborted.
return;
}
this.signal_.isAborted_ = true;
if (this.signal_.onabort_) {
const event = {
'type': 'abort',
'bubbles': false,
'cancelable': false,
'target': this.signal_,
'currentTarget': this.signal_,
};
this.signal_.onabort_(event);
dvoytenko marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
Expand All @@ -39,6 +53,8 @@ class AbortSignal {
constructor() {
/** @private {boolean} */
this.isAborted_ = false;
/** @private {?function(!Event)} */
this.onabort_ = null;
}

/**
Expand All @@ -47,6 +63,20 @@ class AbortSignal {
get aborted() {
return this.isAborted_;
}

/**
* @return {?function(!Event)}
*/
get onabort() {
return this.onabort_;
}

/**
* @param {?function(!Event)} value
*/
set onabort(value) {
this.onabort_ = value;
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/service/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,15 @@ export class Resource {

const promise = new Promise((resolve, reject) => {
Services.vsyncFor(this.hostWin).mutate(() => {
let callbackResult;
try {
resolve(this.element.layoutCallback(signal));
callbackResult = this.element.layoutCallback(signal);
} catch (e) {
reject(e);
}
Promise.resolve(callbackResult).then(resolve, reject);
dvoytenko marked this conversation as resolved.
Show resolved Hide resolved
});
signal.onabort = () => reject(cancellation());
}).then(
() => this.layoutComplete_(true, signal),
(reason) => this.layoutComplete_(false, signal, reason)
Expand Down
74 changes: 74 additions & 0 deletions test/unit/polyfills/test-abort-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright 2020 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 {install} from '../../../src/polyfills/abort-controller';

describes.realWin('AbortController', {}, (env) => {
let win;
let controller, signal;

beforeEach(() => {
win = env.win;

delete win.AbortController;
delete win.AbortSignal;
install(win);

controller = new win.AbortController();
signal = controller.signal;
});

it('should create AbortController and signal', () => {
expect(signal).to.exist;
expect(signal.aborted).to.be.false;
expect(signal.onabort).to.be.null;
});

it('should abort signal without listener', () => {
controller.abort();
expect(signal.aborted).to.be.true;
});

it('should abort signal with listener', () => {
const spy = env.sandbox.spy();
signal.onabort = spy;
expect(signal.onabort).to.equal(spy);

controller.abort();
expect(signal.aborted).to.be.true;
expect(spy).to.be.calledOnce;
const event = spy.firstCall.firstArg;
expect(event).to.contain({
type: 'abort',
bubbles: false,
cancelable: false,
target: signal,
currentTarget: signal,
});
});

it('should only call listener once', () => {
const spy = env.sandbox.spy();
signal.onabort = spy;
expect(signal.onabort).to.equal(spy);

controller.abort();
expect(spy).to.be.calledOnce;

controller.abort();
expect(spy).to.be.calledOnce;
});
});
22 changes: 22 additions & 0 deletions test/unit/test-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {OwnersImpl} from '../../src/service/owners-impl';
import {Resource, ResourceState} from '../../src/service/resource';
import {ResourcesImpl} from '../../src/service/resources-impl';
import {Services} from '../../src/services';
import {isCancellation} from '../../src/error';
import {layoutRectLtwh} from '../../src/layout-rect';

describes.realWin('Resource', {amp: true}, (env) => {
Expand Down Expand Up @@ -621,6 +622,27 @@ describes.realWin('Resource', {amp: true}, (env) => {
expect(resource.getState()).to.equal(ResourceState.LAYOUT_SCHEDULED);
});

it('should abort startLayout with unload', async () => {
const neverEndingPromise = new Promise(() => {});
elementMock.expects('layoutCallback').returns(neverEndingPromise).once();

resource.state_ = ResourceState.LAYOUT_SCHEDULED;
resource.layoutBox_ = {left: 11, top: 12, width: 10, height: 10};
const layoutPromise = resource.startLayout();
expect(resource.getState()).to.equal(ResourceState.LAYOUT_SCHEDULED);

resource.unload();

let error;
try {
await layoutPromise;
} catch (e) {
error = e;
}
expect(error).to.exist;
expect(isCancellation(error)).to.be.true;
});

it('should ignore startLayout for re-layout when not opt-in', () => {
elementMock.expects('layoutCallback').never();

Expand Down