Files
BrowserOS/apps/agent/lib/rpc/RpcClientProvider.tsx
Dani Akash 8657146fb6 fix: reduce suspense boundary depth and improve background color speed (#259)
* fix: move suspense boundary closer to corresponding pages

* fix: pre-resolve the client via singleton to speed up the clientPromise

* feat: apply theme background faster with plain script

* chore: update biome version

* feat: make rpc client persist promise with useMemo and remove loading
text

* fix: replace dvh with vh

* fix: replace dvh with vh in create graph
2026-01-21 23:09:58 +05:30

30 lines
711 B
TypeScript

import { createContext, type FC, type ReactNode, use, useMemo } from 'react'
import { getClient, type RpcClient } from './getClient'
const RpcClientContext = createContext<Promise<RpcClient> | null>(null)
/**
* @public
*/
export const RpcClientProvider: FC<{ children: ReactNode }> = ({
children,
}) => {
const clientPromise = useMemo(() => getClient(), [])
return (
<RpcClientContext.Provider value={clientPromise}>
{children}
</RpcClientContext.Provider>
)
}
/**
* @public
*/
export function useRpcClient(): RpcClient {
const promise = use(RpcClientContext)
if (!promise) {
throw new Error('useRpcClient must be used within RpcClientProvider')
}
return use(promise)
}