Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/core/animations/CoreAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export class CoreAnimation extends EventEmitter {
return;
}

if (this.delayFor <= 0 && this.progress === 0) {
this.emit('start', {});
}

this.progress += dt / duration;

if (this.progress > 1) {
Expand Down
31 changes: 31 additions & 0 deletions src/core/animations/CoreAnimationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import type { CoreAnimation } from './CoreAnimation.js';
import { assertTruthy } from '../../utils.js';

export class CoreAnimationController implements IAnimationController {
startedPromise: Promise<void> | null = null;
/**
* If this is null, then the animation hasn't started yet.
*/
startedResolve: ((scope?: any) => void) | null = null;

stoppedPromise: Promise<void> | null = null;
/**
* If this is null, then the animation is in a finished / stopped state.
Expand All @@ -42,6 +48,9 @@ export class CoreAnimationController implements IAnimationController {
state: AnimationControllerState;

start(): IAnimationController {
this.makeStartedPromise();
this.animation.once('start', this.started.bind(this));

this.makeStoppedPromise();
this.animation.once('finished', this.finished.bind(this));

Expand Down Expand Up @@ -77,13 +86,28 @@ export class CoreAnimationController implements IAnimationController {
return this;
}

waitUntilStarted(): Promise<void> {
this.makeStartedPromise();
const promise = this.startedPromise;
assertTruthy(promise);
return promise;
}

waitUntilStopped(): Promise<void> {
this.makeStoppedPromise();
const promise = this.stoppedPromise;
assertTruthy(promise);
return promise;
}

private makeStartedPromise(): void {
if (this.startedResolve === null) {
this.startedPromise = new Promise((resolve) => {
this.startedResolve = resolve;
});
}
}

private makeStoppedPromise(): void {
if (this.stoppedResolve === null) {
this.stoppedPromise = new Promise((resolve) => {
Expand All @@ -92,6 +116,13 @@ export class CoreAnimationController implements IAnimationController {
}
}

private started(): void {
assertTruthy(this.startedResolve);
// resolve promise (and pass current this to continue to the chain)
this.startedResolve(this);
this.startedResolve = null;
}

private finished(): void {
assertTruthy(this.stoppedResolve);
// If the animation is looping, then we need to restart it.
Expand Down