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

PWA: VIewer as an ampdoc service #5300

Merged
merged 3 commits into from
Oct 3, 2016

Conversation

dvoytenko
Copy link
Contributor

Partial for #3742.
Closes #3406.

@dvoytenko dvoytenko force-pushed the pwa13-viewer3 branch 2 times, most recently from 98f7c59 to 4dbab6a Compare September 30, 2016 01:44
@dvoytenko dvoytenko changed the title WIP PWA: VIewer as an ampdoc service PWA: VIewer as an ampdoc service Sep 30, 2016
@dvoytenko
Copy link
Contributor Author

/to @erwinmombay @aghassemi for review. This change caused a big ripple effect, so please review thoroughly.

@erwinmombay
Copy link
Member

@dvoytenko LGTM, is the overlay code completely gone or has the mechanism moved somewhere?

Copy link
Contributor

@aghassemi aghassemi left a comment

Choose a reason for hiding this comment

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

nothing major except a possible race in canAnimate_ of vsyn since viewerService is initialized asynchronously there.

@@ -41,8 +42,10 @@ const INTERNAL_BRANCHES = {
describe('a4a_config', () => {
let sandbox;
let win;
let ampdoc;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see this actually used outside of beforeEach, maybe inline as installViewerServiceForDoc(ampdocService.getAmpDoc());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

events = {};
installPlatformService(win);
documentStateFor(win);
const attrs = {};
element = {
Copy link
Contributor

Choose a reason for hiding this comment

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

This element is mocked but the one used above isn't. Can we use the same approach for both?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. This is unfortunate. I'm independently working on test rules to avoid this discrepancy. But it's out of scope for this PR.

const ampdocService = ampdocServiceFor(doc.defaultView);
return ampdocService.getAmpDoc(element);
};
doc.body.appendChild(element);
Copy link
Contributor

Choose a reason for hiding this comment

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

does it need to be appended? If so, probably should remove it in afterEach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is tested inside of a hermetic doc. The whole document is thrown out so this is ok.

@@ -466,6 +466,18 @@ function prepareAndAttachShadowDoc(global, extensions, hostElement, doc, url) {
// Instal doc services.
installAmpdocServices(ampdoc);

const viewer = viewerForDoc(ampdoc);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this and the same code in adopt can be refactored to adoptShared

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. This part is executed independently and currently is rather different.

*/
constructor(win) {
constructor(win, ampdoc) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why both? can we do ampdoc.win and remove win param?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to highlight that win is the main argument. ampdoc is a less important thing thing here for now. ampdoc is only used to fix an iOS bug. So as soon as that code gets cleaned up, I will remove it.

/** @private @const {!./ampdoc-impl.AmpDocService} */
this.ampdocService_ = ampdocServiceFor(this.win);

/** @const {!../document-state.DocumentState} */
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

this.forceSchedule_();
}
});
const boundOnVisibilityChanged = this.onVisibilityChanged_.bind(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is confusing me. I was thrown off by docState being a window-level state and not a per-doc state, but that aside, I don't understand why viewer.onVisibilityChanged is more efficient than docState_.onVisibilityChanged in the singleDoc mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically, when window == ampdoc, viewer's visibility property can be used to determine visibility for both. In this case it's more efficient to disable animations when ampdoc is not visible. In multi-doc mode, we can only use window signal.

return false;
}

// Single doc: animations allowed when single doc is visible.
Copy link
Contributor

@aghassemi aghassemi Oct 3, 2016

Choose a reason for hiding this comment

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

This may have race issue since singleDocViewer_ is initialized asynchronously so it may or may not be null based on timing. Is it guaranteed to have viewerService instantiated by this point? if not, this may either return true or fail if there is a contextNode and viewerForDoc returns null because viewerService is not instantiated yet. If we are guaranteed to have a viewerService instance by this point, can we simplify the code to:

if (this.docState_.isHidden()) {
 return false;
}

// getAmpDoc() handles opt_contextNode and caching the singleDoc just fine itself.

const ampdoc = this.ampdocService_.getAmpDoc(opt_contextNode);

// is there really a need to cache the viewer instance for single viewer case?  I assume `viewerForDoc` and therefore `getServiceForDoc` do proper caching of instances anyway.

return viewerForDoc(ampdoc).isVisible();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be fine. When doc is available, the viewer is guaranteed to be available. The risk condition is ok as well - it will simply yield the same instance in this case. The only place where viewer might not be available is the constructor above.

@dvoytenko
Copy link
Contributor Author

@erwinmombay The overlay code as used to be defined in BaseElement has been deprecated couple of month ago. It's never provided by the Viewport class. This is simply a cleanup on that deprecation.

@aghassemi
Copy link
Contributor

LGTM!

@dvoytenko dvoytenko merged commit 1ba0160 into ampproject:master Oct 3, 2016
@dvoytenko dvoytenko deleted the pwa13-viewer3 branch October 3, 2016 20:42
dreamofabear pushed a commit to dreamofabear/amphtml that referenced this pull request Oct 4, 2016
* Make Viewer an ampdoc service

* review fixes

* lints
dreamofabear pushed a commit to dreamofabear/amphtml that referenced this pull request Oct 4, 2016
* Make Viewer an ampdoc service

* review fixes

* lints
dreamofabear pushed a commit to dreamofabear/amphtml that referenced this pull request Oct 12, 2016
* Make Viewer an ampdoc service

* review fixes

* lints
mityaha pushed a commit to ooyala/amphtml that referenced this pull request Nov 30, 2016
* Make Viewer an ampdoc service

* review fixes

* lints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants