import { P, match, type Rule } from "matchigo";
type Event = { kind: "click"; x: number } | { kind: "key"; key: string };
const RULES: ReadonlyArray<Rule<Event, string>> = [
{ with: { kind: "click" }, then: (e) => `click@${e.x}` },
{ with: { kind: "key", key: "Escape" }, then: () => "escape" },
{ with: { kind: "key" }, then: (e) => `key:${e.key}` },
];
match({ kind: "key", key: "a" } as Event, RULES); // "key:a"
Data-driven dispatch — matches
valueagainst each rule in order and runs the first rule whosewithmatches. For the hot path, hoistrulesto module scope; the compiled dispatcher is cached on the array identity, so inline rebuilds miss the cache (and trigger a dev warning).