implement() function

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

The implement() function types call handlers against a schema’s calls and returns them as given, so handlers kept in their own files can be spread into defineRoom()’s calls.

Syntax

implement(schema, handlers)

Parameters

schema
The Schema that declares the calls.
handlers
An object with a handler for any of the schema’s calls. Each takes the args first, then { room, connection }, and returns or resolves to the reply.

Return value

handlers, unchanged.

Description

implement() does nothing at runtime; it’s there for the types. defineRoom() still reports a declared call with no handler, so spreading several files’ handlers, calls: { ...shop, ...combat }, checks that every call has one.

Examples

Handlers split across files

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

export const Hero = entity("hero", { hp: "u16", gold: "u32" });
export const schema = defineSchema({
  private: Hero,
  calls: defineCalls({ heal: call().returns<number>(), loot: call({ gold: "u16" }) }),
});
// healer.ts
import { implement } from "@irtio/room";
import { schema } from "./schema.js";

export const healer = implement(schema, {
  heal(_args, { room, connection }) {
    const hero = room.private(connection);
    hero.hp = Math.min(hero.hp + 25, 100);
    return hero.hp;
  },
});
// dungeon.ts
import { defineRoom } from "@irtio/room";
import { healer } from "./healer.js";
import { schema } from "./schema.js";

export default defineRoom({
  schema,
  calls: {
    ...healer,
    loot({ gold }, { room, connection }) {
      room.private(connection).gold += gold;
    },
  },
});

See also

Runtime availability

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