refactor(animations): add an onStart handler for AnimationPlayer (#10360)

This commit is contained in:
Matias Niemelä
2016-08-22 16:39:52 -07:00
committed by Kara
parent 23a27776e2
commit 3c561475c8
10 changed files with 114 additions and 28 deletions

View File

@ -16,7 +16,8 @@ import {getDOM} from './dom_adapter';
import {DomAnimatePlayer} from './dom_animate_player';
export class WebAnimationsPlayer implements AnimationPlayer {
private _subscriptions: Function[] = [];
private _onDoneFns: Function[] = [];
private _onStartFns: Function[] = [];
private _finished = false;
private _initialized = false;
private _player: DomAnimatePlayer;
@ -37,8 +38,8 @@ export class WebAnimationsPlayer implements AnimationPlayer {
if (!isPresent(this.parentPlayer)) {
this.destroy();
}
this._subscriptions.forEach(fn => fn());
this._subscriptions = [];
this._onDoneFns.forEach(fn => fn());
this._onDoneFns = [];
}
}
@ -66,10 +67,17 @@ export class WebAnimationsPlayer implements AnimationPlayer {
return <DomAnimatePlayer>element.animate(keyframes, options);
}
onDone(fn: Function): void { this._subscriptions.push(fn); }
onStart(fn: () => void): void { this._onStartFns.push(fn); }
onDone(fn: () => void): void { this._onDoneFns.push(fn); }
play(): void {
this.init();
if (!this.hasStarted()) {
this._onStartFns.forEach(fn => fn());
this._onStartFns = [];
this._started = true;
}
this._player.play();
}

View File

@ -128,5 +128,16 @@ export function main() {
expect(captures2['finish'].length).toEqual(1);
expect(captures2['cancel'].length).toEqual(0);
});
it('should run the onStart method when started but only once', () => {
var calls = 0;
player.onStart(() => calls++);
expect(calls).toEqual(0);
player.play();
expect(calls).toEqual(1);
player.pause();
player.play();
expect(calls).toEqual(1);
});
});
}