fix(platform-browser): update started state on reset (#41608)

This commit fixes the state of variable _started on call of reset method.

Earlier behaviour was on call of `reset()` method we are not setting back
`_started` flag's value to false and it created various issue for end user
which were expecting this behaviour as per name of method.

We provided end user `reset()` method, but it was empty method and on call
of it wasn't doing anything. As end user/developer were not able to
reuse animation not animation after call of reset method.

In this PR on call of `reset()` method we are setting flag `_started` value to false
which can be accessed by `hasStarted()`.

Resolves #18140

PR Close #41608
This commit is contained in:
iRealNirmal 2021-04-14 09:37:11 +05:30 committed by Zach Arend
parent d7768c61ad
commit 3a6af8e629
6 changed files with 50 additions and 2 deletions

View File

@ -31,4 +31,5 @@ export declare class MockAnimationPlayer extends NoopAnimationPlayer {
finish(): void; finish(): void;
hasStarted(): boolean; hasStarted(): boolean;
play(): void; play(): void;
reset(): void;
} }

View File

@ -15,6 +15,7 @@ const DEFAULT_FILL_MODE = 'forwards';
const DEFAULT_EASING = 'linear'; const DEFAULT_EASING = 'linear';
export const enum AnimatorControlState { export const enum AnimatorControlState {
RESET = 0,
INITIALIZED = 1, INITIALIZED = 1,
STARTED = 2, STARTED = 2,
FINISHED = 3, FINISHED = 3,
@ -26,7 +27,6 @@ export class CssKeyframesPlayer implements AnimationPlayer {
private _onStartFns: Function[] = []; private _onStartFns: Function[] = [];
private _onDestroyFns: Function[] = []; private _onDestroyFns: Function[] = [];
private _started = false;
// TODO(issue/24571): remove '!'. // TODO(issue/24571): remove '!'.
private _styler!: ElementAnimationStyleHandler; private _styler!: ElementAnimationStyleHandler;
@ -139,6 +139,7 @@ export class CssKeyframesPlayer implements AnimationPlayer {
this.play(); this.play();
} }
reset(): void { reset(): void {
this._state = AnimatorControlState.RESET;
this._styler.destroy(); this._styler.destroy();
this._buildStyler(); this._buildStyler();
this._styler.apply(); this._styler.apply();

View File

@ -83,6 +83,11 @@ export class MockAnimationPlayer extends NoopAnimationPlayer {
this._onInitFns = []; this._onInitFns = [];
} }
reset() {
super.reset();
this.__started = false;
}
finish(): void { finish(): void {
super.finish(); super.finish();
this.__finished = true; this.__finished = true;

View File

@ -184,7 +184,9 @@ export class NoopAnimationPlayer implements AnimationPlayer {
this._onDestroyFns = []; this._onDestroyFns = [];
} }
} }
reset(): void {} reset(): void {
this._started = false;
}
setPosition(position: number): void { setPosition(position: number): void {
this._position = this.totalTime ? position * this.totalTime : 1; this._position = this.totalTime ? position * this.totalTime : 1;
} }

View File

@ -104,6 +104,7 @@ export class RendererAnimationPlayer implements AnimationPlayer {
reset(): void { reset(): void {
this._command('reset'); this._command('reset');
this._started = false;
} }
setPosition(p: number): void { setPosition(p: number): void {

View File

@ -104,5 +104,43 @@ import {el} from '../../testing/src/browser_util';
expect(finished).toBeTruthy(); expect(finished).toBeTruthy();
expect(destroyed).toBeTruthy(); expect(destroyed).toBeTruthy();
})); }));
it('should update `hasStarted()` on `play()` and `reset()`', fakeAsync(() => {
@Component({
selector: 'ani-another-cmp',
template: '...',
})
class CmpAnother {
@ViewChild('target') public target: any;
constructor(public builder: AnimationBuilder) {}
build() {
const definition =
this.builder.build([style({opacity: 0}), animate(1000, style({opacity: 1}))]);
return definition.create(this.target);
}
}
TestBed.configureTestingModule({declarations: [CmpAnother]});
const fixture = TestBed.createComponent(CmpAnother);
const cmp = fixture.componentInstance;
fixture.detectChanges();
const player = cmp.build();
expect(player.hasStarted()).toBeFalsy();
flushMicrotasks();
player.play();
flushMicrotasks();
expect(player.hasStarted()).toBeTruthy();
player.reset();
flushMicrotasks();
expect(player.hasStarted()).toBeFalsy();
}));
}); });
} }