refactor(animations): improve _finalKeyframe non-null assertion (#44458)

instead of declaring the WebAnimationsPlayer's _finalKeyframe
field with a non-null assertion, declare it as optional field
and use a non-null assertion only when it is being used in the
beforeDestroy function

relates to #24571

PR Close #44458
This commit is contained in:
dario-piotrowicz
2021-12-12 18:31:50 +00:00
committed by Alex Rickabaugh
parent 5da31d6d25
commit f56ac0fe83
@@ -22,8 +22,7 @@ export class WebAnimationsPlayer implements AnimationPlayer {
private _finished = false;
private _started = false;
private _destroyed = false;
// TODO(issue/24571): remove '!'.
private _finalKeyframe!: {[key: string]: string|number};
private _finalKeyframe?: {[key: string]: string|number};
// TODO(issue/24571): remove '!'.
public readonly domPlayer!: DOMAnimation;
@@ -173,10 +172,13 @@ export class WebAnimationsPlayer implements AnimationPlayer {
beforeDestroy() {
const styles: {[key: string]: string|number} = {};
if (this.hasStarted()) {
Object.keys(this._finalKeyframe).forEach(prop => {
// note: this code is invoked only when the `play` function was called prior to this
// (thus `hasStarted` returns true), this implies that the code that initializes
// `_finalKeyframe` has also been executed and the non-null assertion can be safely used here
const finalKeyframe = this._finalKeyframe!;
Object.keys(finalKeyframe).forEach(prop => {
if (prop != 'offset') {
styles[prop] =
this._finished ? this._finalKeyframe[prop] : computeStyle(this.element, prop);
styles[prop] = this._finished ? finalKeyframe[prop] : computeStyle(this.element, prop);
}
});
}