From f56ac0fe83b1cdc9b98ec8dfb5e22f06adc0884d Mon Sep 17 00:00:00 2001 From: dario-piotrowicz Date: Sun, 12 Dec 2021 18:31:50 +0000 Subject: [PATCH] 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 --- .../render/web_animations/web_animations_player.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/animations/browser/src/render/web_animations/web_animations_player.ts b/packages/animations/browser/src/render/web_animations/web_animations_player.ts index b0b0caa6617..89cc3bff363 100644 --- a/packages/animations/browser/src/render/web_animations/web_animations_player.ts +++ b/packages/animations/browser/src/render/web_animations/web_animations_player.ts @@ -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); } }); }