import type { Bridge, BridgeStore, ExtractStore } from "@webview-bridge/types"; import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-selector"; export function useBridge( store: BridgeStore, ): ExtractStore>; export function useBridge< T extends Bridge, U extends ExtractStore>, V, >(store: BridgeStore, selector?: (state: U) => V): V; export function useBridge( store: BridgeStore, selector?: (state: T) => U, ): U { const $selector = selector ?? ((state: T) => state as unknown as U); return useSyncExternalStoreWithSelector( store.subscribe, store.getState, store.getState, $selector, ); }