Skip to content

Commit

Permalink
Add helper function for creating local store client (#32600)
Browse files Browse the repository at this point in the history
...and decrease boilerplate outside of the library

GitOrigin-RevId: 0e5f91b87ddfac1ee7a38da75fb561630efb35b4
  • Loading branch information
sshader authored and Convex, Inc. committed Dec 23, 2024
1 parent 93d7ada commit 2fa8d00
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion npm-packages/local-store/react/LocalStoreProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
NoopLocalPersistence,
} from "../browser/localPersistence";
import { MutationRegistry } from "./mutationRegistry";

import { ConvexReactClient } from "convex/react";
import { Election } from "../browser/worker/election";
export const LocalStoreContext = createContext<LocalStoreClient | null>(null);

type LocalStoreProviderProps<SyncSchema extends SchemaDefinition<any, any>> =
Expand Down Expand Up @@ -69,3 +70,34 @@ export function useLocalStoreClient(): LocalStoreClient {
}
return localStoreClient;
}

export function createLocalStoreClient(opts: {
syncSchema: SchemaDefinition<any, any>;
mutationRegistry: MutationRegistry<any>;
convexClient: ConvexReactClient;
convexUrl: string;
persistenceKey: string | null;
}) {
const persistence = opts.persistenceKey
? new Election(opts.persistenceKey, opts.convexUrl)
: new NoopLocalPersistence();
const logger = new Logger();
const mutationMap = opts.mutationRegistry.exportToMutationMap();
const coreLocalStore = new CoreSyncEngine(
opts.syncSchema,
mutationMap,
logger,
);
const driver = new Driver({
coreLocalStore,
network: new NetworkImpl({ convexClient: opts.convexClient.sync }),
localPersistence: persistence ?? new NoopLocalPersistence(),
logger,
});
const localStore = new LocalStoreClient({
driver,
syncSchema: opts.syncSchema,
mutations: mutationMap,
});
return localStore;
}

0 comments on commit 2fa8d00

Please sign in to comment.