call() function

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The call() function declares one call and the fields of its args, for defineCalls().

Syntax

call()
call(fields)

Parameters

fields Optional
An object of the args’ fields, each a field type as entity() takes, but "bytes", or a declaration from json<T>() without an initial value or string(max). Without it, the call takes no args.

Return value

A CallDeclaration. Its returns<T>() declares the reply’s type.

Exceptions

TypeError
Thrown for a bytes field, a json field with an initial value, or an unknown field type.
RangeError
Thrown for a string(max) with a max out of range.

Description

The room checks a call declared with fields before its handler runs: each field must be there and pass the check a write of its field type gets, and an f32, f64 or angle must also be finite. Keys that aren’t declared are dropped. A call declared with call() gets undefined whatever the client sends.

Examples

Calls with and without args

// inventory.ts
import { call, defineCalls, defineRoom, defineSchema, entity, json } from "@irtio/room";

const Bag = entity("bag", { items: json<string[]>([]) });
const schema = defineSchema({
  private: Bag,
  calls: defineCalls({
    drop: call({ slot: "u8" }).returns<string[]>(),
    sort: call({ order: json<"name" | "rarity">() }),
    contents: call().returns<string[]>(),
  }),
});

export default defineRoom({
  schema,
  calls: {
    drop({ slot }, { room, connection }) {
      const bag = room.private(connection);
      bag.items = bag.items.filter((_item, i) => i !== slot);
      return bag.items;
    },
    sort({ order }, { room, connection }) {
      const bag = room.private(connection);
      if (order === "name") bag.items = [...bag.items].sort();
    },
    contents: (_args, { room, connection }) => room.private(connection).items,
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
call()0.10.10.10.1