{"version":3,"sources":["../src/ipc.ts"],"sourcesContent":["// Inter-app messaging (UI_AS_APPS_SPEC §5.6) — L2: chrome panels/regions talk to\n// each other across an edge BOTH sides declared in their bindings (sender `ipc.to`\n// + the `ipc` capability; receiver `ipc.accepts`). The host enforces both halves\n// and attaches an unspoofable `from`; you still treat the payload as untrusted.\nimport { useEffect, useState } from 'react';\nimport { throwOnRefusal } from './protocolRefusal';\nimport { protocolRequest, addListener } from './sandboxUtils';\nimport { PROTOCOL_IPC, REGION_MESSAGE } from './generated/protocol';\nimport { SCHEMES } from './protocolSchemes';\n\n/** A message delivered to this region. `from` is the SENDER's region, attached by\n *  the host (unspoofable, T19); `data` is the sender-provided payload. */\nexport interface RegionMessage {\n  from: string;\n  data: unknown;\n}\n\n/**\n * Send a message to another region. Resolves when delivered; rejects (`forbidden`)\n * if the edge isn't two-sided-consented, the target isn't mounted, or this app\n * lacks the `ipc` capability.\n */\nexport const postToRegion = async (region: string, data: unknown): Promise<void> => {\n  const res = (await protocolRequest(SCHEMES[PROTOCOL_IPC], 'post', [{ to: region, msg: data }])) as\n    | { ok: true }\n    | { ok: false; code?: string; message?: string }\n    | undefined;\n  throwOnRefusal(res, 'ipc post failed');\n};\n\n/**\n * Ask the host to bring the user to `region` — the `reveal`-class method of\n * UI_AS_APPS_SPEC §4.1's focus model (R3-243). On mobile that is the column\n * transition (the panel's carousel advances to the main pane); on desktop a focus\n * move. Use it when a tap in your panel produces something to look at in another\n * column, right after the `postToRegion` that sends it there:\n *\n * ```ts\n * onClick={() => {                                   // a REAL user gesture\n *   void postToRegion('stage.conversation', { type: 'select-conversation', id });\n *   void revealRegion('stage.conversation');\n * }}\n * ```\n *\n * **Call it synchronously inside a user-gesture handler.** The host reads its own\n * transient user activation and refuses without one — an app flipping columns on a\n * timer is the attention-steal primitive §4.1 is written against. It reaches only\n * regions you already have a two-sided IPC edge to, and only within the active\n * activity, so it can neither jump the user into an unrelated surface nor pull\n * focus back to the caller's own column.\n *\n * Resolves once the host has *considered* the request — deliberately NOT reporting\n * whether focus actually moved. Whether the user is moved is the host's call\n * (activation, rate limit, already-there), and an app that could observe the\n * refusal would be tempted to retry, which is the behaviour the gate exists to\n * stop. Rejects only on a genuine authorization failure (`forbidden`): no `ipc`\n * capability, or no declared edge to `region`.\n */\nexport const revealRegion = async (region: string): Promise<void> => {\n  const res = (await protocolRequest(SCHEMES[PROTOCOL_IPC], 'reveal', [{ to: region }])) as\n    | { ok: true }\n    | { ok: false; code?: string; message?: string }\n    | undefined;\n  throwOnRefusal(res, 'ipc reveal failed');\n};\n\n/** Subscribe to inbound region messages. Returns an unsubscribe fn. */\nexport const onRegionMessage = (listener: (msg: RegionMessage) => void): (() => void) =>\n  addListener(REGION_MESSAGE, (m: { from: string; data: unknown }) => listener({ from: m.from, data: m.data }));\n\n/** React hook: the most recent inbound region message (or `null`). */\nexport const useRegionMessage = (): RegionMessage | null => {\n  const [msg, setMsg] = useState<RegionMessage | null>(null);\n  useEffect(() => onRegionMessage(setMsg), []);\n  return msg;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,mBAAoC;AACpC,6BAA+B;AAC/B,0BAA6C;AAC7C,sBAA6C;AAC7C,6BAAwB;AAcjB,MAAM,eAAe,OAAO,QAAgB,SAAiC;AAClF,QAAM,MAAO,UAAM,qCAAgB,+BAAQ,4BAAY,GAAG,QAAQ,CAAC,EAAE,IAAI,QAAQ,KAAK,KAAK,CAAC,CAAC;AAI7F,6CAAe,KAAK,iBAAiB;AACvC;AA8BO,MAAM,eAAe,OAAO,WAAkC;AACnE,QAAM,MAAO,UAAM,qCAAgB,+BAAQ,4BAAY,GAAG,UAAU,CAAC,EAAE,IAAI,OAAO,CAAC,CAAC;AAIpF,6CAAe,KAAK,mBAAmB;AACzC;AAGO,MAAM,kBAAkB,CAAC,iBAC9B,iCAAY,gCAAgB,CAAC,MAAuC,SAAS,EAAE,MAAM,EAAE,MAAM,MAAM,EAAE,KAAK,CAAC,CAAC;AAGvG,MAAM,mBAAmB,MAA4B;AAC1D,QAAM,CAAC,KAAK,MAAM,QAAI,uBAA+B,IAAI;AACzD,8BAAU,MAAM,gBAAgB,MAAM,GAAG,CAAC,CAAC;AAC3C,SAAO;AACT;","names":[]}