@tanstack/svelte-db
Version:
Svelte integration for @tanstack/db
22 lines (21 loc) • 638 B
JavaScript
import { getContext, setContext } from 'svelte';
const dbClientContext = Symbol.for(`@tanstack/svelte-db.DbClient`);
export function setDbClientContext(client) {
return setContext(dbClientContext, client);
}
export function useDbClient() {
const client = useOptionalDbClient();
if (!client) {
throw new Error(`useDbClient must be used within a DbProvider.`);
}
return client;
}
export function useOptionalDbClient() {
try {
return getContext(dbClientContext)?.();
}
catch {
// Legacy helpers may be called from a rune root rather than a component.
return undefined;
}
}