Skip to content

Commit dc1f52b

Browse files
committed
fix(animations): properly handle cancelled animation style application
1 parent 4168024 commit dc1f52b

7 files changed

Lines changed: 194 additions & 74 deletions

File tree

‎packages/animations/browser/src/render/transition_animation_engine.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {ENTER_CLASSNAME, LEAVE_CLASSNAME, NG_ANIMATING_CLASSNAME, NG_ANIMATING_S
1818
import {AnimationDriver} from './animation_driver';
1919
import {getOrSetAsInMap, listenOnPlayer, makeAnimationEvent, normalizeKeyframes, optimizeGroupPlayer} from './shared';
2020

21-
const EMPTY_PLAYER_ARRAY: AnimationPlayer[] = [];
21+
const EMPTY_PLAYER_ARRAY: TransitionAnimationPlayer[] = [];
2222
const NULL_REMOVAL_STATE: ElementAnimationState = {
2323
namespaceId: '',
2424
setForRemoval: null,
@@ -708,7 +708,14 @@ export class TransitionAnimationEngine {
708708

709709
if (this._namespaceList.length &&
710710
(this.totalQueuedPlayers || this.collectedLeaveElements.length)) {
711-
players = this._flushAnimations(microtaskId);
711+
const cleanupFns: Function[] = [];
712+
try {
713+
players = this._flushAnimations(cleanupFns, microtaskId);
714+
} finally {
715+
for (let i = 0; i < cleanupFns.length; i++) {
716+
cleanupFns[i]();
717+
}
718+
}
712719
} else {
713720
for (let i = 0; i < this.collectedLeaveElements.length; i++) {
714721
const element = this.collectedLeaveElements[i];
@@ -737,15 +744,15 @@ export class TransitionAnimationEngine {
737744
}
738745
}
739746

740-
private _flushAnimations(microtaskId: number): TransitionAnimationPlayer[] {
747+
private _flushAnimations(cleanupFns: Function[], microtaskId: number):
748+
TransitionAnimationPlayer[] {
741749
const subTimelines = new ElementInstructionMap();
742750
const skippedPlayers: TransitionAnimationPlayer[] = [];
743751
const skippedPlayersMap = new Map<any, AnimationPlayer[]>();
744752
const queuedInstructions: QueuedTransition[] = [];
745753
const queriedElements = new Map<any, TransitionAnimationPlayer[]>();
746754
const allPreStyleElements = new Map<any, Set<string>>();
747755
const allPostStyleElements = new Map<any, Set<string>>();
748-
const cleanupFns: Function[] = [];
749756

750757
const bodyNode = getBodyNode();
751758
const allEnterNodes: any[] = this.collectedEnterElements.length ?
@@ -855,7 +862,6 @@ export class TransitionAnimationEngine {
855862
instruction.errors !.forEach(error => { msg += `- ${error}\n`; });
856863
});
857864

858-
cleanupFns.forEach(fn => fn());
859865
allPlayers.forEach(player => player.destroy());
860866
throw new Error(msg);
861867
}
@@ -1011,7 +1017,6 @@ export class TransitionAnimationEngine {
10111017
player.play();
10121018
});
10131019

1014-
cleanupFns.forEach(fn => fn());
10151020
return rootPlayers;
10161021
}
10171022

@@ -1107,20 +1112,16 @@ export class TransitionAnimationEngine {
11071112
const allSubElements = new Set<any>();
11081113
const allNewPlayers = instruction.timelines.map(timelineInstruction => {
11091114
const element = timelineInstruction.element;
1115+
allConsumedElements.add(element);
11101116

11111117
// FIXME (matsko): make sure to-be-removed animations are removed properly
11121118
const details = element[REMOVAL_FLAG];
11131119
if (details && details.removedBeforeQueried) return new NoopAnimationPlayer();
11141120

11151121
const isQueriedElement = element !== rootElement;
1116-
let previousPlayers: AnimationPlayer[] = EMPTY_PLAYER_ARRAY;
1117-
if (!allConsumedElements.has(element)) {
1118-
allConsumedElements.add(element);
1119-
const _previousPlayers = allPreviousPlayersMap.get(element);
1120-
if (_previousPlayers) {
1121-
previousPlayers = _previousPlayers.map(p => p.getRealPlayer());
1122-
}
1123-
}
1122+
const previousPlayers =
1123+
(allPreviousPlayersMap.get(element) || EMPTY_PLAYER_ARRAY).map(p => p.getRealPlayer());
1124+
11241125
const preStyles = preStylesMap.get(element);
11251126
const postStyles = postStylesMap.get(element);
11261127
const keyframes = normalizeKeyframes(

‎packages/animations/browser/src/render/web_animations/web_animations_player.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import {AnimationPlayer} from '@angular/animations';
99

10-
import {copyStyles, eraseStyles, setStyles} from '../../util';
10+
import {allowPreviousPlayerStylesMerge, copyStyles} from '../../util';
1111

1212
import {DOMAnimation} from './dom_animation';
1313

@@ -26,7 +26,7 @@ export class WebAnimationsPlayer implements AnimationPlayer {
2626
public time = 0;
2727

2828
public parentPlayer: AnimationPlayer|null = null;
29-
public previousStyles: {[styleName: string]: string | number};
29+
public previousStyles: {[styleName: string]: string | number} = {};
3030
public currentSnapshot: {[styleName: string]: string | number} = {};
3131

3232
constructor(
@@ -37,11 +37,12 @@ export class WebAnimationsPlayer implements AnimationPlayer {
3737
this._delay = <number>options['delay'] || 0;
3838
this.time = this._duration + this._delay;
3939

40-
this.previousStyles = {};
41-
previousPlayers.forEach(player => {
42-
let styles = player.currentSnapshot;
43-
Object.keys(styles).forEach(prop => this.previousStyles[prop] = styles[prop]);
44-
});
40+
if (allowPreviousPlayerStylesMerge(this._duration, this._delay)) {
41+
previousPlayers.forEach(player => {
42+
let styles = player.currentSnapshot;
43+
Object.keys(styles).forEach(prop => this.previousStyles[prop] = styles[prop]);
44+
});
45+
}
4546
}
4647

4748
private _onFinish() {

‎packages/animations/browser/src/util.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,7 @@ const DASH_CASE_REGEXP = /-+([a-z0-9])/g;
213213
export function dashCaseToCamelCase(input: string): string {
214214
return input.replace(DASH_CASE_REGEXP, (...m: any[]) => m[1].toUpperCase());
215215
}
216+
217+
export function allowPreviousPlayerStylesMerge(duration: number, delay: number) {
218+
return duration === 0 || delay === 0;
219+
}

‎packages/animations/browser/testing/src/mock_animation_driver.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {AUTO_STYLE, AnimationPlayer, NoopAnimationPlayer, ɵStyleData} from '@an
99

1010
import {AnimationDriver} from '../../src/render/animation_driver';
1111
import {containsElement, invokeQuery, matchesElement} from '../../src/render/shared';
12-
12+
import {allowPreviousPlayerStylesMerge} from '../../src/util';
1313

1414
/**
1515
* @experimental Animation support is experimental.
@@ -56,12 +56,15 @@ export class MockAnimationPlayer extends NoopAnimationPlayer {
5656
public duration: number, public delay: number, public easing: string,
5757
public previousPlayers: any[]) {
5858
super();
59-
previousPlayers.forEach(player => {
60-
if (player instanceof MockAnimationPlayer) {
61-
const styles = player.currentSnapshot;
62-
Object.keys(styles).forEach(prop => this.previousStyles[prop] = styles[prop]);
63-
}
64-
});
59+
60+
if (allowPreviousPlayerStylesMerge(duration, delay)) {
61+
previousPlayers.forEach(player => {
62+
if (player instanceof MockAnimationPlayer) {
63+
const styles = player.currentSnapshot;
64+
Object.keys(styles).forEach(prop => this.previousStyles[prop] = styles[prop]);
65+
}
66+
});
67+
}
6568

6669
this.totalTime = delay + duration;
6770
}

‎packages/core/test/animation/animation_integration_spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ export function main() {
782782
expect(players.length).toEqual(3);
783783
const [p1, p2, p3] = players;
784784
expect(p1.previousStyles).toEqual({opacity: AUTO_STYLE});
785-
expect(p2.previousStyles).toEqual({});
785+
expect(p2.previousStyles).toEqual({opacity: AUTO_STYLE});
786786
expect(p3.previousStyles).toEqual({});
787787
});
788788

‎packages/core/test/animation/animation_query_integration_spec.ts‎

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,9 @@ export function main() {
10001000
}
10011001
});
10021002

1003-
it('should properly cancel items that were queried into a former animation', () => {
1004-
@Component({
1003+
it('should properly cancel items that were queried into a former animation and pass in the associated styles into the follow-up players per element',
1004+
() => {
1005+
@Component({
10051006
selector: 'ani-cmp',
10061007
template: `
10071008
<div [@myAnimation]="exp" class="parent">
@@ -1024,45 +1025,45 @@ export function main() {
10241025
])]
10251026
})
10261027
class Cmp {
1027-
public exp: any;
1028-
public items: any[];
1029-
}
1028+
public exp: any;
1029+
public items: any[];
1030+
}
10301031

1031-
TestBed.configureTestingModule({declarations: [Cmp]});
1032+
TestBed.configureTestingModule({declarations: [Cmp]});
10321033

1033-
const engine = TestBed.get(ɵAnimationEngine);
1034-
const fixture = TestBed.createComponent(Cmp);
1035-
const cmp = fixture.componentInstance;
1034+
const engine = TestBed.get(ɵAnimationEngine);
1035+
const fixture = TestBed.createComponent(Cmp);
1036+
const cmp = fixture.componentInstance;
10361037

1037-
cmp.exp = 'on';
1038-
cmp.items = [0, 1, 2, 3, 4];
1039-
fixture.detectChanges();
1040-
engine.flush();
1038+
cmp.exp = 'on';
1039+
cmp.items = [0, 1, 2, 3, 4];
1040+
fixture.detectChanges();
1041+
engine.flush();
10411042

1042-
const previousPlayers = getLog();
1043-
expect(previousPlayers.length).toEqual(10);
1044-
resetLog();
1043+
const previousPlayers = getLog();
1044+
expect(previousPlayers.length).toEqual(10);
1045+
resetLog();
10451046

1046-
cmp.exp = 'off';
1047-
cmp.items = [0, 1, 2];
1048-
fixture.detectChanges();
1049-
engine.flush();
1047+
cmp.exp = 'off';
1048+
cmp.items = [0, 1, 2];
1049+
fixture.detectChanges();
1050+
engine.flush();
10501051

1051-
const players = getLog();
1052-
expect(players.length).toEqual(4);
1052+
const players = getLog();
1053+
expect(players.length).toEqual(4);
10531054

1054-
const [p1, p2, p3, p4] = players;
1055-
const [p1p1, p1p2] = p1.previousPlayers;
1056-
const [p2p1, p2p2] = p2.previousPlayers;
1055+
const [p1, p2, p3, p4] = players;
10571056

1058-
expect(p1p1).toBe(previousPlayers[3]);
1059-
expect(p1p2).toBe(previousPlayers[8]);
1060-
expect(p2p1).toBe(previousPlayers[4]);
1061-
expect(p2p2).toBe(previousPlayers[9]);
1057+
// p1 && p2 are the starting players for item3 and item4
1058+
expect(p1.previousStyles)
1059+
.toEqual({opacity: AUTO_STYLE, width: AUTO_STYLE, height: AUTO_STYLE});
1060+
expect(p2.previousStyles)
1061+
.toEqual({opacity: AUTO_STYLE, width: AUTO_STYLE, height: AUTO_STYLE});
10621062

1063-
expect(p3.previousPlayers).toEqual([]);
1064-
expect(p4.previousPlayers).toEqual([]);
1065-
});
1063+
// p3 && p4 are the following players for item3 and item4
1064+
expect(p3.previousStyles).toEqual({});
1065+
expect(p4.previousStyles).toEqual({});
1066+
});
10661067

10671068
it('should not remove a parent container if its contents are queried into by an ancestor element',
10681069
() => {

0 commit comments

Comments
 (0)