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

R1: schedule amp-bind targets #37619

Merged
merged 1 commit into from
Feb 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,11 @@ function createBaseCustomElementClass(win, elementConnectedCallback) {
mutatedAttributesCallback(mutations) {
if (this.impl_) {
this.impl_.mutatedAttributesCallback(mutations);
} else if (this.R1()) {
// If amp-bind is trying to mutate an uninitialized BaseElement, it is probably about time
// to initialize it.
const scheduler = getSchedulerForDoc(this);
scheduler.scheduleAsap(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's this different than a component setting deferredMount = false when it needs to mutate before mount?

Copy link
Member Author

@samouri samouri Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, this affects every component instead of only a specific one.

Second, the mount of the component (build/layout) will now be deferred until the amp-bind event occurs instead of on document load. Especially for an initially hidden component it may make sense to defer initialization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this allow us to remove deferredMount = false from the components that define it?

Copy link
Member Author

@samouri samouri Feb 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only component that does so is amp-sidebar. Answer is maybe. Unsure right now why deferredMount=false was added to begin with.

}
}

Expand Down
8 changes: 8 additions & 0 deletions test/unit/test-custom-element-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,14 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
await element.buildInternal();
await promise;
});

it('should build if amp-bind mutation', async () => {
const element = new ElementClass();
builderMock.expects('scheduleAsap').withExactArgs(element).once();

doc.body.appendChild(element);
element.mutatedAttributesCallback({});
});
});

describe('mount/unmount', () => {
Expand Down