| 1 | import { useEffect } from "react"; |
| 2 | import { onRemoteTabOpened, onRemoteTabUpdated } from "./bridge"; |
| 3 | import type { TabMeta } from "./types"; |
| 4 | import { createSubscriptionScope } from "./subscriptionScope"; |
| 5 | |
| 6 | export function useRemoteTabOpened( |
| 7 | registerTabMeta: (tab: TabMeta) => void, |
| 8 | updateTabMeta: (tab: TabMeta) => void, |
| 9 | ) { |
| 10 | useEffect(() => { |
| 11 | const scope = createSubscriptionScope(); |
| 12 | scope.listen(onRemoteTabOpened, (meta) => { |
| 13 | if (!meta?.id || !meta.remote) return; |
| 14 | // Events are resource notifications. Only a request-owned navigation |
| 15 | // completion may adopt the surface, even if this event arrives first. |
| 16 | registerTabMeta(meta); |
| 17 | }); |
| 18 | scope.listen(onRemoteTabUpdated, (meta) => { |
| 19 | if (!meta?.id || !meta.remote) return; |
| 20 | updateTabMeta(meta); |
| 21 | }); |
| 22 | return () => scope.dispose(); |
| 23 | }, [registerTabMeta, updateTabMeta]); |
| 24 | } |
| 25 |