11"use client" ;
22
33import { useMemo } from "react" ;
4- import { restoreReplaneClient } from "@replanejs/sdk" ;
4+ import { Replane } from "@replanejs/sdk" ;
55import { ReplaneContext } from "./context" ;
66import { useReplaneClientInternal , useReplaneClientSuspense } from "./useReplaneClient" ;
77import { useStateful } from "./hooks" ;
@@ -27,7 +27,7 @@ function ReplaneProviderWithClient<T extends object>({
2727
2828/**
2929 * Internal provider component for restoring client from snapshot.
30- * Uses restoreReplaneClient which is synchronous .
30+ * Creates a Replane client synchronously and connects in background .
3131 */
3232function ReplaneProviderWithSnapshot < T extends object > ( {
3333 options,
@@ -37,22 +37,27 @@ function ReplaneProviderWithSnapshot<T extends object>({
3737 snapshot : NonNullable < ReplaneProviderWithOptionsProps < T > [ "snapshot" ] > ;
3838} ) {
3939 const client = useStateful (
40- ( ) =>
41- restoreReplaneClient < T > ( {
40+ ( ) => {
41+ const replane = new Replane < T > ( {
4242 snapshot,
43- connection : {
44- baseUrl : options . baseUrl ,
45- sdkKey : options . sdkKey ,
46- fetchFn : options . fetchFn ,
47- requestTimeoutMs : options . requestTimeoutMs ,
48- retryDelayMs : options . retryDelayMs ,
49- inactivityTimeoutMs : options . inactivityTimeoutMs ,
50- logger : options . logger ,
51- agent : options . agent ?? DEFAULT_AGENT ,
52- } ,
43+ logger : options . logger ,
5344 context : options . context ,
54- } ) ,
55- ( c ) => c . close ( ) ,
45+ defaults : options . defaults ,
46+ } ) ;
47+ // Start connection in background (don't await)
48+ replane . connect ( {
49+ baseUrl : options . baseUrl ,
50+ sdkKey : options . sdkKey ,
51+ fetchFn : options . fetchFn ,
52+ requestTimeoutMs : options . requestTimeoutMs ,
53+ retryDelayMs : options . retryDelayMs ,
54+ inactivityTimeoutMs : options . inactivityTimeoutMs ,
55+ connectTimeoutMs : options . connectTimeoutMs ,
56+ agent : options . agent ?? DEFAULT_AGENT ,
57+ } ) ;
58+ return replane ;
59+ } ,
60+ ( c ) => c . disconnect ( ) ,
5661 [ snapshot , options ]
5762 ) ;
5863 const value = useMemo < ReplaneContextValue < T > > ( ( ) => ( { client } ) , [ client ] ) ;
@@ -96,13 +101,14 @@ function ReplaneProviderWithSuspense<T extends object>({
96101}
97102
98103/**
99- * Provider component that makes a ReplaneClient available to the component tree.
104+ * Provider component that makes a Replane client available to the component tree.
100105 *
101- * Can be used in three ways:
106+ * Can be used in several ways:
102107 *
103108 * 1. With a pre-created client:
104109 * ```tsx
105- * const client = await createReplaneClient({ ... });
110+ * const client = new Replane({ defaults: { ... } });
111+ * await client.connect({ baseUrl: '...', sdkKey: '...' });
106112 * <ReplaneProvider client={client}>
107113 * <App />
108114 * </ReplaneProvider>
0 commit comments