{"version":3,"file":"machineInspector.mjs","sources":["../../../src/utils/machineInspector.ts"],"sourcesContent":["/**\n * Opt-in registry that lets a development tool observe the state machines driving the connection\n * layer.\n *\n * The machines are private to the objects that own them and, more importantly, do not outlive them:\n * a full reconnect replaces the whole engine along with its `SignalClient`, so anything watching a\n * single machine reference goes blind exactly when the interesting part starts. Machines therefore\n * announce themselves here as they are constructed, and a subscriber sees the whole succession.\n *\n * Nothing is recorded until {@link enableMachineInspector} is called, which no shipping code does —\n * with the registry off, announcing is a comparison and a return.\n */\n\n/**\n * The slice of machina's `Fsm` surface an inspector needs. Structural rather than machina's own\n * type so that any machine satisfies it regardless of its state and input unions.\n */\nexport interface InspectableMachine {\n  readonly id: string;\n  readonly initialState: string;\n  readonly states: Record<string, Record<string, unknown>>;\n  readonly context?: unknown;\n  currentState(): string;\n  canHandle(input: string): boolean;\n  on(eventName: string, callback: (data: any) => void): { off(): void };\n}\n\nexport interface MachineAnnouncement {\n  /** What the machine drives, e.g. `signal`. Not unique: each new `SignalClient` announces one. */\n  label: string;\n  /** How many machines have been announced before this one, so successive instances are tellable apart. */\n  seq: number;\n  /**\n   * When the machine was constructed. Carried on the announcement because subscribers receive the\n   * recorded ones on subscribe, and stamping those at replay time would date them all to whenever\n   * the tool happened to open.\n   */\n  at: number;\n  machine: InspectableMachine;\n}\n\n/** Kept small: enough for a panel opened mid-session to see how the connection got where it is. */\nconst HISTORY_LIMIT = 16;\n\nlet enabled = false;\nlet announced: MachineAnnouncement[] = [];\nlet subscribers: Array<(announcement: MachineAnnouncement) => void> = [];\n\n/**\n * Starts recording machine announcements. Call before the machines of interest are constructed —\n * for the signal machine that means before the `Room` is created.\n */\nexport function enableMachineInspector() {\n  enabled = true;\n}\n\nexport function isMachineInspectorEnabled() {\n  return enabled;\n}\n\n/** Announces a machine to whatever is watching. A no-op unless the inspector was enabled. */\nexport function announceMachine(label: string, machine: InspectableMachine) {\n  if (!enabled) {\n    return;\n  }\n  const announcement: MachineAnnouncement = {\n    label,\n    seq: announced.length,\n    at: Date.now(),\n    machine,\n  };\n  announced = [...announced.slice(-(HISTORY_LIMIT - 1)), announcement];\n  for (const subscriber of subscribers) {\n    subscriber(announcement);\n  }\n}\n\n/**\n * Subscribes to machine announcements, replaying the ones already recorded so that a panel opened\n * after connecting still sees the machine currently in charge. Returns an unsubscribe function.\n */\nexport function onMachineAnnounced(callback: (announcement: MachineAnnouncement) => void) {\n  subscribers = [...subscribers, callback];\n  for (const announcement of announced) {\n    callback(announcement);\n  }\n  return () => {\n    subscribers = subscribers.filter((subscriber) => subscriber !== callback);\n  };\n}\n"],"names":["announceMachine","label","machine"],"mappings":"AAAA;;;;;;;;;;;AAWG;AA8BH;AAmBA;AACM,SAAUA,eAAeA,CAACC,KAAa,EAAEC,OAA2B,EAAA;EAC1D;AACZ,IAAA;AACF,EAAA;AAWF"}