Skip to content

Commit

Permalink
try to reconnect after disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
capaj committed Jan 25, 2024
1 parent c8e5836 commit 4962f66
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion web-extension/src/content-script/connectTRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,28 @@ import { AppRouter } from '../background/chromeRuntimeListener'
import { chromeLink } from '@capaj/trpc-browser/link'
import browser from 'webextension-polyfill'

const connectTRPC = () => {
const port = browser.runtime.connect()

trpc = createTRPCProxyClient<AppRouter>({
// @ts-expect-error
links: [chromeLink({ port })]
})

port.onDisconnect.addListener(() => {
connectTRPC() // recursively call to reconnect
})

return trpc
}

const port = browser.runtime.connect()

export const trpc = createTRPCProxyClient<AppRouter>({
export let trpc = createTRPCProxyClient<AppRouter>({
// @ts-expect-error
links: [chromeLink({ port })]
})

port.onDisconnect.addListener(() => {
connectTRPC() // recursively call to reconnect
})

0 comments on commit 4962f66

Please sign in to comment.