Skip to content

Function: extractMachineRoutes()

Documentation / @xmachines/play-router / extractMachineRoutes

function extractMachineRoutes(machine): RouteTree;

Defined in: extract-routes.ts:45

Extract complete route tree from state machine graph

Crawls machine starting from root, visits all state nodes (including nested and parallel), extracts meta.route metadata, validates route references, and builds hierarchical tree.

Parameters

ParameterTypeDescription
machineAnyStateMachineXState v5 state machine

Returns

RouteTree

Route tree with root, byStateId map, and byPath map

Throws

If route references non-existent state

Throws

If route path is malformed (doesn’t start with /)

Throws

If parallel states define conflicting routes

Example

import { createMachine } from "xstate";
import { extractMachineRoutes } from "@xmachines/play-router";
const machine = createMachine({
initial: "home",
states: {
home: { id: "home", meta: { route: "/" } },
dashboard: {
id: "dashboard",
meta: { route: "/dashboard" },
initial: "overview",
states: {
overview: { id: "overview", meta: { route: "/overview" } },
},
},
},
});
const tree = extractMachineRoutes(machine);
console.log(tree.byPath.get("/dashboard/overview"));