call() function
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe call() function declares one call and the fields of its args, for defineCalls().
Syntax
call()
call(fields)
Parameters
fieldsOptional- An object of the args’ fields, each a field type as
entity()takes, but"bytes", or a declaration fromjson<T>()without an initial value orstring(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
bytesfield, ajsonfield with an initial value, or an unknown field type. RangeError- Thrown for a
string(max)with amaxout 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 rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
call() | 0.1 | 0.1 | 0.1 | 0.1 |