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

Move owners implementation from resources to owners #23668

Merged
merged 3 commits into from
Aug 26, 2019
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
8 changes: 8 additions & 0 deletions build-system/tasks/presubmit-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,14 @@ const forbiddenTerms = {
'src/service/viewer-impl.js',
],
},
'\\.findResourcesInElements\\(': {
message: 'findResourcesInElements is a restricted API.',
whitelist: ['src/service/owners-impl.js'],
},
'\\.measureAndTryScheduleLayout\\(': {
message: 'measureAndTryScheduleLayout is a restricted API.',
whitelist: ['src/service/owners-impl.js'],
},
'(win|Win)(dow)?(\\(\\))?\\.open\\W': {
message: 'Use dom.openWindowDialog',
whitelist: ['src/dom.js'],
Expand Down
6 changes: 2 additions & 4 deletions src/inabox/inabox-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import {installHistoryServiceForDoc} from '../service/history-impl';
import {installIframeMessagingClient} from './inabox-iframe-messaging-client';
import {installInaboxCidService} from './inabox-cid';
import {installInaboxViewportService} from './inabox-viewport';
import {
installOwnersServiceForDoc,
installResourcesServiceForDoc,
} from '../service/resources-impl';
import {installOwnersServiceForDoc} from '../service/owners-impl';
import {installResourcesServiceForDoc} from '../service/resources-impl';
import {installStandardActionsForDoc} from '../service/standard-actions-impl';
import {installUrlForDoc} from '../service/url-impl';
import {installUrlReplacementsServiceForDoc} from '../service/url-replacements-impl';
Expand Down
6 changes: 2 additions & 4 deletions src/service/core-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ import {installHistoryServiceForDoc} from './history-impl';
import {installImg} from '../../builtins/amp-img';
import {installInputService} from '../input';
import {installLayout} from '../../builtins/amp-layout';
import {
installOwnersServiceForDoc,
installResourcesServiceForDoc,
} from './resources-impl';
import {installOwnersServiceForDoc} from './owners-impl';
import {installPixel} from '../../builtins/amp-pixel';
import {installPlatformService} from './platform-impl';
import {installResourcesServiceForDoc} from './resources-impl';
import {installStandardActionsForDoc} from './standard-actions-impl';
import {installStorageServiceForDoc} from './storage-impl';
import {installTemplatesService} from './template-impl';
Expand Down
174 changes: 170 additions & 4 deletions src/service/owners-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,30 @@
*/

/* eslint-disable no-unused-vars */

// TODO(powerivq)
// Resource.setOwner, Resource.getOwner should be moved here.
// ResourceState.NOT_BUILT might not be needed here.
import {Resource, ResourceState} from './resource';
powerivq marked this conversation as resolved.
Show resolved Hide resolved
import {Services} from '../services';
import {devAssert} from '../log';
import {isArray} from '../types';
import {registerServiceBuilderForDoc} from '../service';

/**
* @param {!Element|!Array<!Element>} elements
* @return {!Array<!Element>}
*/
function elements(elements) {
return /** @type {!Array<!Element>} */ (isArray(elements)
? elements
: [elements]);
}

/**
* TODO(lannka): migrate all methods from OwnersDef to here.
*
* @interface
*/
export class Owners {
export class OwnersDef {
Copy link
Contributor

@lannka lannka Aug 16, 2019

Choose a reason for hiding this comment

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

We decided to unify all the service interface names to XXXInterface, and move it out to its own file owners-interface.js

/**
* Assigns an owner for the specified element. This means that the resources
* within this element will be managed by the owner and not Resources manager.
Expand Down Expand Up @@ -90,4 +108,152 @@ export class Owners {
*/
updateInViewport(parentElement, subElements, inLocalViewport) {}
}
/* eslint-enable no-unused-vars */

/**
* @implements {OwnersDef}
* @visibleForTesting
*/
export class Owners {
Copy link
Contributor

Choose a reason for hiding this comment

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

@visibleForTesting

Owners => OwnersImpl

/**
* @param {!./ampdoc-impl.AmpDoc} ampdoc
*/
constructor(ampdoc) {
/** @const @private {!./resources-impl.ResourcesDef} */
this.resources_ = Services.resourcesForDoc(ampdoc);
}

/** @override */
setOwner(element, owner) {
Resource.setOwner(element, owner);
}

/** @override */
schedulePreload(parentElement, subElements) {
this.scheduleLayoutOrPreloadForSubresources_(
this.resources_.getResourceForElement(parentElement),
/* layout */ false,
elements(subElements)
);
}

/** @override */
scheduleLayout(parentElement, subElements) {
this.scheduleLayoutOrPreloadForSubresources_(
this.resources_.getResourceForElement(parentElement),
/* layout */ true,
elements(subElements)
);
}

/** @override */
schedulePause(parentElement, subElements) {
const parentResource = this.resources_.getResourceForElement(parentElement);
subElements = elements(subElements);

this.resources_.findResourcesInElements(
parentResource,
subElements,
resource => {
resource.pause();
}
);
}

/** @override */
scheduleResume(parentElement, subElements) {
const parentResource = this.resources_.getResourceForElement(parentElement);
subElements = elements(subElements);

this.resources_.findResourcesInElements(
parentResource,
subElements,
resource => {
resource.resume();
}
);
}

/** @override */
scheduleUnlayout(parentElement, subElements) {
const parentResource = this.resources_.getResourceForElement(parentElement);
subElements = elements(subElements);

this.resources_.findResourcesInElements(
parentResource,
subElements,
resource => {
resource.unlayout();
}
);
}

/** @override */
updateInViewport(parentElement, subElements, inLocalViewport) {
this.updateInViewportForSubresources_(
this.resources_.getResourceForElement(parentElement),
elements(subElements),
inLocalViewport
);
}

/**
* Schedules layout or preload for the sub-resources of the specified
* resource.
* @param {!Resource} parentResource
* @param {boolean} layout
* @param {!Array<!Element>} subElements
* @private
*/
scheduleLayoutOrPreloadForSubresources_(parentResource, layout, subElements) {
this.resources_.findResourcesInElements(
parentResource,
subElements,
resource => {
if (resource.getState() === ResourceState.NOT_BUILT) {
resource.whenBuilt().then(() => {
this.resources_.measureAndTryScheduleLayout(
resource,
!layout,
parentResource.getLayoutPriority()
);
});
} else {
this.resources_.measureAndTryScheduleLayout(
resource,
!layout,
parentResource.getLayoutPriority()
);
}
}
);
}

/**
* Updates inViewport state for the specified sub-resources of a resource.
* @param {!Resource} parentResource
* @param {!Array<!Element>} subElements
* @param {boolean} inLocalViewport
* @private
*/
updateInViewportForSubresources_(
parentResource,
subElements,
inLocalViewport
) {
const inViewport = parentResource.isInViewport() && inLocalViewport;
this.resources_.findResourcesInElements(
parentResource,
subElements,
resource => {
resource.setInViewport(inViewport);
}
);
}
}

/**
* @param {!./ampdoc-impl.AmpDoc} ampdoc
*/
export function installOwnersServiceForDoc(ampdoc) {
registerServiceBuilderForDoc(ampdoc, 'owners', Owners);
}
Loading