Skip to content

Commit

Permalink
refactor: await setTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB committed Oct 9, 2023
1 parent d18501c commit 7ff92b4
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/components/sbb-clock/sbb-clock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ export class SbbClock extends LitElement {
/** Move the minutes hand every minute. */
private _handMovement: ReturnType<typeof setInterval>;

private _handlePageVisibilityChange(): void {
private async _handlePageVisibilityChange(): Promise<void> {
if (document.visibilityState === 'hidden') {
this._stopClock();
} else if (!this._hasDataNow()) {
this._startClock();
await this._startClock();
}
}

Expand Down Expand Up @@ -235,7 +235,7 @@ export class SbbClock extends LitElement {
}

/** Starts the clock by defining the hands starting position then starting the animations. */
private _startClock(): void {
private async _startClock(): Promise<void> {
this._clockHandHours?.addEventListener(
'animationend',
this._moveHoursHandFn,
Expand All @@ -247,7 +247,9 @@ export class SbbClock extends LitElement {
ADD_EVENT_LISTENER_OPTIONS,
);

setTimeout(() => this._setHandsStartingPosition(), INITIAL_TIMEOUT_DURATION);
await new Promise(() =>
setTimeout(() => this._setHandsStartingPosition(), INITIAL_TIMEOUT_DURATION),
);
}

private _hasDataNow(): boolean {
Expand All @@ -262,13 +264,13 @@ export class SbbClock extends LitElement {
return new Date();
}

protected override firstUpdated(): void {
protected override async firstUpdated(): Promise<void> {
this._addEventListeners();

if (this._hasDataNow()) {
this._stopClock();
} else {
this._startClock();
await this._startClock();
}
}

Expand Down

0 comments on commit 7ff92b4

Please sign in to comment.