Skip to content

Commit

Permalink
refactor(animations): make AnimationDriver.getParentElement required
Browse files Browse the repository at this point in the history
This change is a follow up to angular#45057 to make `AnimationDriver.getParentElement`
a required method, which allows removing the slow path for the animation
namespace insertion logic.

BREAKING CHANGE:

The `AnimationDriver.getParentElement` method has become required, so any
implementors of this interface are now required to provide an implementation
for this method. This breakage is unlikely to affect application developers,
as `AnimationDriver` is not expected to be implemented in user code.
  • Loading branch information
JoostK committed Feb 16, 2022
1 parent db4a658 commit e742127
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/animations/browser/src/render/animation_driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export abstract class AnimationDriver {
* be required to implement this method. This method is to become required in a major version of
* Angular.
*/
abstract getParentElement?(element: unknown): unknown;
abstract getParentElement(element: unknown): unknown;

abstract query(element: any, selector: string, multi: boolean): any[];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,34 +594,20 @@ export class TransitionAnimationEngine {
const limit = namespaceList.length - 1;
if (limit >= 0) {
let found = false;
if (this.driver.getParentElement !== undefined) {
// Fast path for when the driver implements `getParentElement`, which allows us to find the
// closest ancestor with an existing namespace that we can then insert `ns` after, without
// having to inspect all existing namespaces.
let ancestor = this.driver.getParentElement(hostElement);
while (ancestor) {
const ancestorNs = namespacesByHostElement.get(ancestor);
if (ancestorNs) {
// An animation namespace has been registered for this ancestor, so we insert `ns`
// right after it to establish top-down ordering of animation namespaces.
const index = namespaceList.indexOf(ancestorNs);
namespaceList.splice(index + 1, 0, ns);
found = true;
break;
}
ancestor = this.driver.getParentElement(ancestor);
}
} else {
// Slow path for backwards compatibility if the driver does not implement
// `getParentElement`, to be removed once `getParentElement` is a required method.
for (let i = limit; i >= 0; i--) {
const nextNamespace = namespaceList[i];
if (this.driver.containsElement(nextNamespace.hostElement, hostElement)) {
namespaceList.splice(i + 1, 0, ns);
found = true;
break;
}
// Find the closest ancestor with an existing namespace so we can then insert `ns` after it,
// establishing a top-down ordering of namespaces in `this._namespaceList`.
let ancestor = this.driver.getParentElement(hostElement);
while (ancestor) {
const ancestorNs = namespacesByHostElement.get(ancestor);
if (ancestorNs) {
// An animation namespace has been registered for this ancestor, so we insert `ns`
// right after it to establish top-down ordering of animation namespaces.
const index = namespaceList.indexOf(ancestorNs);
namespaceList.splice(index + 1, 0, ns);
found = true;
break;
}
ancestor = this.driver.getParentElement(ancestor);
}
if (!found) {
// No namespace exists that is an ancestor of `ns`, so `ns` is inserted at the front to
Expand Down

0 comments on commit e742127

Please sign in to comment.