Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions __tests__/unit/dom/animation.promises.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
Expand Down
25 changes: 10 additions & 15 deletions packages/g/src/plugins/web-animations-api/dom/Animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand All @@ -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') {
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
Loading