Skip to content

Commit

Permalink
Move owners implementation from resources to owners (ampproject#23668)
Browse files Browse the repository at this point in the history
* Move owners implementation from resources to owners

* Refactor discoverElements

* Fix tests
  • Loading branch information
powerivq authored Aug 26, 2019
1 parent 1b20457 commit 39abb12
Show file tree
Hide file tree
Showing 8 changed files with 595 additions and 479 deletions.
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';
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 {
/**
* 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 {
/**
* @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

0 comments on commit 39abb12

Please sign in to comment.