Skip to content

Commit

Permalink
fix(animations): allow animations to be destroyed manually
Browse files Browse the repository at this point in the history
  • Loading branch information
matsko committed Nov 7, 2016
1 parent f3793b5 commit 3e91a65
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 104 deletions.
23 changes: 14 additions & 9 deletions modules/@angular/compiler/src/animation/animation_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,22 @@ class _AnimationBuilder implements AnimationAstVisitor {
_ANIMATION_PLAYER_VAR
.callMethod(
'onDone',
[o.fn(
[],
[RENDER_STYLES_FN
.callFn([
_ANIMATION_FACTORY_ELEMENT_VAR, _ANIMATION_FACTORY_RENDERER_VAR,
o.importExpr(resolveIdentifier(Identifiers.prepareFinalAnimationStyles))
[o
.fn([],
[
_ANIMATION_PLAYER_VAR.callMethod('destroy', []).toStmt(),
RENDER_STYLES_FN
.callFn([
_ANIMATION_START_STATE_STYLES_VAR, _ANIMATION_END_STATE_STYLES_VAR
_ANIMATION_FACTORY_ELEMENT_VAR, _ANIMATION_FACTORY_RENDERER_VAR,
o.importExpr(
resolveIdentifier(Identifiers.prepareFinalAnimationStyles))
.callFn([
_ANIMATION_START_STATE_STYLES_VAR,
_ANIMATION_END_STATE_STYLES_VAR
])
])
])
.toStmt()])])
.toStmt()
])])
.toStmt());

statements.push(_ANIMATION_FACTORY_VIEW_CONTEXT
Expand Down
3 changes: 0 additions & 3 deletions modules/@angular/core/src/animation/animation_group_player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ export class AnimationGroupPlayer implements AnimationPlayer {
private _onFinish() {
if (!this._finished) {
this._finished = true;
if (!isPresent(this.parentPlayer)) {
this.destroy();
}
this._onDoneFns.forEach(fn => fn());
this._onDoneFns = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ export class AnimationSequencePlayer implements AnimationPlayer {
private _onFinish() {
if (!this._finished) {
this._finished = true;
if (!isPresent(this.parentPlayer)) {
this.destroy();
}
this._onDoneFns.forEach(fn => fn());
this._onDoneFns = [];
}
Expand Down
61 changes: 21 additions & 40 deletions modules/@angular/core/test/animation/animation_group_player_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function main() {
assertLastStatus(players[2], 'restart', true);
});

it('should finish all the players', () => {
it('should not destroy the inner the players when finished', () => {
var group = new AnimationGroupPlayer(players);

var completed = false;
Expand All @@ -113,55 +113,36 @@ export function main() {

group.finish();

assertLastStatus(players[0], 'finish', true, -1);
assertLastStatus(players[1], 'finish', true, -1);
assertLastStatus(players[2], 'finish', true, -1);

assertLastStatus(players[0], 'destroy', true);
assertLastStatus(players[1], 'destroy', true);
assertLastStatus(players[2], 'destroy', true);
assertLastStatus(players[0], 'finish', true);
assertLastStatus(players[1], 'finish', true);
assertLastStatus(players[2], 'finish', true);

expect(completed).toBeTruthy();
});

it('should call destroy automatically when finished if no parent player is present', () => {
var group = new AnimationGroupPlayer(players);

group.play();

assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);

group.finish();

assertLastStatus(players[0], 'destroy', true);
assertLastStatus(players[1], 'destroy', true);
assertLastStatus(players[2], 'destroy', true);
});

it('should not call destroy automatically when finished if a parent player is present', () => {
var group = new AnimationGroupPlayer(players);
var parent = new AnimationGroupPlayer([group, new MockAnimationPlayer()]);
it('should not call destroy automatically when finished even if a parent player finishes',
() => {
var group = new AnimationGroupPlayer(players);
var parent = new AnimationGroupPlayer([group, new MockAnimationPlayer()]);

group.play();
group.play();

assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);
assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);

group.finish();
group.finish();

assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);
assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);

parent.finish();
parent.finish();

assertLastStatus(players[0], 'destroy', true);
assertLastStatus(players[1], 'destroy', true);
assertLastStatus(players[2], 'destroy', true);
});
assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);
});

it('should function without any players', () => {
var group = new AnimationGroupPlayer([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,55 +135,36 @@ export function main() {

sequence.finish();

assertLastStatus(players[0], 'finish', true, -1);
assertLastStatus(players[1], 'finish', true, -1);
assertLastStatus(players[2], 'finish', true, -1);

assertLastStatus(players[0], 'destroy', true);
assertLastStatus(players[1], 'destroy', true);
assertLastStatus(players[2], 'destroy', true);
assertLastStatus(players[0], 'finish', true);
assertLastStatus(players[1], 'finish', true);
assertLastStatus(players[2], 'finish', true);

expect(completed).toBeTruthy();
});

it('should call destroy automatically when finished if no parent player is present', () => {
var sequence = new AnimationSequencePlayer(players);

sequence.play();

assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);

sequence.finish();

assertLastStatus(players[0], 'destroy', true);
assertLastStatus(players[1], 'destroy', true);
assertLastStatus(players[2], 'destroy', true);
});

it('should not call destroy automatically when finished if a parent player is present', () => {
var sequence = new AnimationSequencePlayer(players);
var parent = new AnimationSequencePlayer([sequence, new MockAnimationPlayer()]);
it('should not call destroy automatically when finished even if a parent player is present',
() => {
var sequence = new AnimationSequencePlayer(players);
var parent = new AnimationSequencePlayer([sequence, new MockAnimationPlayer()]);

sequence.play();
sequence.play();

assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);
assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);

sequence.finish();
sequence.finish();

assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);
assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);

parent.finish();
parent.finish();

assertLastStatus(players[0], 'destroy', true);
assertLastStatus(players[1], 'destroy', true);
assertLastStatus(players[2], 'destroy', true);
});
assertLastStatus(players[0], 'destroy', false);
assertLastStatus(players[1], 'destroy', false);
assertLastStatus(players[2], 'destroy', false);
});

it('should function without any players', () => {
var sequence = new AnimationSequencePlayer([]);
Expand Down
4 changes: 0 additions & 4 deletions modules/@angular/core/testing/mock_animation_player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import {AnimationPlayer} from '@angular/core';
import {isPresent} from './facade/lang';

export class MockAnimationPlayer implements AnimationPlayer {
private _onDoneFns: Function[] = [];
Expand All @@ -27,9 +26,6 @@ export class MockAnimationPlayer implements AnimationPlayer {

this._onDoneFns.forEach(fn => fn());
this._onDoneFns = [];
if (!isPresent(this.parentPlayer)) {
this.destroy();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import {AUTO_STYLE} from '@angular/core';
import {isPresent} from '../facade/lang';
import {AnimationPlayer} from '../private_import_core';

import {getDOM} from './dom_adapter';
Expand All @@ -33,9 +32,6 @@ export class WebAnimationsPlayer implements AnimationPlayer {
private _onFinish() {
if (!this._finished) {
this._finished = true;
if (!isPresent(this.parentPlayer)) {
this.destroy();
}
this._onDoneFns.forEach(fn => fn());
this._onDoneFns = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ export function main() {
expect(count).toEqual(2);
});

it('should destroy itself automatically if a parent player is not present', () => {
it('should not destroy itself automatically if a parent player is not present', () => {
captures['cancel'] = [];
player.finish();

expect(captures['finish'].length).toEqual(1);
expect(captures['cancel'].length).toEqual(1);
expect(captures['cancel'].length).toEqual(0);

var next = makePlayer();
var player2 = next['player'];
Expand Down

0 comments on commit 3e91a65

Please sign in to comment.