EntityStore: all() method

Available since @irtio/client 5.0BrowsersNode

The all() method of the EntityStore interface returns an iterator over the entities of a kind in the client’s view.

Syntax

all(kind)

Parameters

kind
A kind, the object entity() returned.

Return value

An iterator over the kind’s entities the client holds, each typed as its kind, in the order they entered. It’s empty for a kind the client holds none of, a hidden kind included.

Exceptions

None.

Description

all() visits only what the room sends this client, which the room’s interest decides, not every entity of the kind in the room.

Iterate it with for...of, or spread it into an array, [...room.entities.all(kind)], to count or sort the entities. The iterator reads the store as it goes, so don’t keep one across state updates.

To find one entity, use get() rather than searching all().

Examples

Drawing a leaderboard of the players in view

Each racer is an entity with a lap count. The client sorts the racers it holds each time a state update arrives.

// standings.ts
import { connect, defineSchema, entity } from "@irtio/client";

const Racer = entity("racer", { name: "string", laps: "u8" });
const schema = defineSchema({ entities: [Racer] });

const room = await connect("track-2", { schema });

room.addEventListener("update", () => {
  const standings = [...room.entities.all(Racer)].sort((a, b) => b.laps - a.laps);
  for (const [place, racer] of standings.entries()) console.log(`${place + 1}. ${racer.name} (${racer.laps} laps)`);
});

See also

Runtime availability

BrowsersNode
all()5.05.0