From 428862d7baa3e9923d06f429a33e7f495f23854e Mon Sep 17 00:00:00 2001 From: lanqing431 <359744414+lanqing431@users.noreply.github.com> Date: Sat, 28 Nov 2026 04:31:21 -0500 Subject: [PATCH] fix(animation): settle promises after cancellation --- __tests__/unit/dom/animation.promises.spec.ts | 26 +++++++++++++++++++ .../web-animations-api/dom/Animation.ts | 25 +++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/__tests__/unit/dom/animation.promises.spec.ts b/__tests__/unit/dom/animation.promises.spec.ts index 62b2ff26e..36eaa4521 100644 --- a/__tests__/unit/dom/animation.promises.spec.ts +++ b/__tests__/unit/dom/animation.promises.spec.ts @@ -244,6 +244,7 @@ describe('Animation promises', () => { "2 finishedEarly is finished", "2 seeked is finished", "2 seekedBack is finished", + "2 cancelled finished rejected", "2 finishedEarly finished finished", "2 seeked finished finished", "2 seekedBack finished finished", @@ -263,6 +264,31 @@ describe('Animation promises', () => { `); }); + it.each([0, 100])( + 'should reject finished when cancelled before playback starts (delay %i)', + async (delay) => { + const animation = animate({ delay }); + const finished = animation.finished; + const rejected = jest.fn(); + const readyRejected = jest.fn(); + finished.catch(rejected); + animation.ready.catch(readyRejected); + + animation.cancel(); + await Promise.resolve(); + + expect(animation.playState).toBe('idle'); + expect(animation.pending).toBe(false); + expect(rejected).toHaveBeenCalledWith( + expect.objectContaining({ name: 'AbortError' }), + ); + expect(readyRejected).toHaveBeenCalledWith( + expect.objectContaining({ name: 'AbortError' }), + ); + expect(animation.finished).not.toBe(finished); + }, + ); + it('should settle the promises of animations started from promise callbacks', async () => { const a = animate({ duration: 32 }); watch('a', a); diff --git a/packages/g/src/plugins/web-animations-api/dom/Animation.ts b/packages/g/src/plugins/web-animations-api/dom/Animation.ts index 358bebde5..c593bce17 100644 --- a/packages/g/src/plugins/web-animations-api/dom/Animation.ts +++ b/packages/g/src/plugins/web-animations-api/dom/Animation.ts @@ -32,8 +32,9 @@ export class Animation implements IAnimation { */ get pending() { return ( - (this._startTime === null && !this._paused && this.playbackRate !== 0) || - this.currentTimePending + !this._idle && + ((this._startTime === null && !this._paused && this.playbackRate !== 0) || + this.currentTimePending) ); } private currentTimePending = false; @@ -86,7 +87,7 @@ export class Animation implements IAnimation { resolve(this); }; this.rejectReadyPromise = () => { - reject(new Error()); + reject(new DOMException('Animation cancelled', 'AbortError')); }; }); if (!this.pending) { @@ -121,7 +122,7 @@ export class Animation implements IAnimation { resolve(this); }; this.rejectFinishedPromise = () => { - reject(new Error()); + reject(new DOMException('Animation cancelled', 'AbortError')); }; }); if (this.playState === 'finished') { @@ -271,6 +272,7 @@ export class Animation implements IAnimation { const newPlayState = this.pending ? 'pending' : this.playState; if (this.readyPromise && newPlayState !== oldPlayState) { if (newPlayState === 'idle') { + this.readyPromise.catch(() => {}); this.rejectReadyPromise(); this.readyPromise = undefined; } else if (oldPlayState === 'pending') { @@ -281,6 +283,8 @@ export class Animation implements IAnimation { } if (this.finishedPromise && newPlayState !== oldPlayState) { if (newPlayState === 'idle') { + // The Web Animations specification marks a cancelled promise as handled. + this.finishedPromise.catch(() => {}); this.rejectFinishedPromise(); this.finishedPromise = undefined; } else if (newPlayState === 'finished') { @@ -344,12 +348,13 @@ export class Animation implements IAnimation { cancel() { this.updatePromises(); - if (!this._inEffect) return; + if (this._idle) return; this._inEffect = false; this._idle = true; this._paused = false; this._finishedFlag = true; + this.currentTimePending = false; this._currentTime = null; this._startTime = null; this.effect.update(null); @@ -359,16 +364,6 @@ export class Animation implements IAnimation { this.updatePromises(); - /** - * 1. Reject the current finished promise with a DOMException named "AbortError". - * 2. Let current finished promise be a new promise - * @see https://w3c.github.io/csswg-drafts/web-animations-1/#canceling-an-animation-section - */ - // if (this.finishedPromise) { - // this.rejectFinishedPromise(); - // this.finishedPromise = undefined; - // } - if (this.oncancel) { const event = new AnimationEvent( null,