RoomClient: cancelCountdown() method

Available since @irtio/client 5.0BrowsersNode

The cancelCountdown() method of the RoomClient interface cancels a lobby’s countdown, as the lobby’s leader. It’s the lobby’s call cancel.

Syntax

cancelCountdown()

Parameters

None.

Return value

A Promise that resolves with undefined once the countdown has stopped.

Exceptions

IrtError

Rejects with this if the call fails. Its name says why:

OperationError
The lobby refused, and its reason says why: this player isn’t the leader, or the lobby isn’t counting down. In a room that isn’t a lobby, the room has no cancel call to answer.
TimeoutError
The lobby didn’t answer within 10 seconds.
NetworkError
The connection dropped before the answer, or the client is reconnecting.
InvalidStateError
The client hasn’t been ready yet, or it’s closed.

Description

Cancelling puts the lobby back in the "waiting" phase and clears every player’s ready flag, so the start rule that started the countdown doesn’t start it again at once. Players mark themselves ready again with setLobbyReady(), or the leader calls startCountdown().

room.lobby changes, and the lobby event fires.

Examples

Cancelling from the countdown screen

During the countdown, the leader sees a cancel button with the seconds left, counted on the room’s clock with serverTime().

// countdown.ts
import { connect, IrtError } from "@irtio/client";

const room = await connect("lobby", { type: "lobby" });
const button = document.querySelector<HTMLButtonElement>("#cancel")!;

function frame() {
  const startsAt = room.lobby?.startsAt;
  const now = room.serverTime();
  const leading = room.presence.leader === room.player;
  button.hidden = !leading || room.lobby?.phase !== "countdown";
  if (startsAt && now !== undefined) button.textContent = `Cancel (${Math.ceil((startsAt - now) / 1000)})`;
  requestAnimationFrame(frame);
}
frame();

button.addEventListener("click", async () => {
  try {
    await room.cancelCountdown();
  } catch (e) {
    // The countdown may have ended while the call was on its way
    if (e instanceof IrtError && e.name === "OperationError") console.warn(e.reason);
    else throw e;
  }
});

See also

Runtime availability

BrowsersNode
cancelCountdown()5.05.0