import { CompilableQuery } from '@powersync/common';
import { AdditionalOptions, DifferentialHookOptions } from '../watched/watch-types.js';
import { ReadonlySuspenseQueryResult, SuspenseQueryResult } from './SuspenseQueryResult.js';
import { useSingleSuspenseQuery } from './useSingleSuspenseQuery.js';
import { useWatchedSuspenseQuery } from './useWatchedSuspenseQuery.js';
/**
* A hook to access the results of a watched query that suspends until the initial result has loaded.
* @example
* export const ContentComponent = () => {
* // The lists array here will be a new Array reference whenever a change to the
* // lists table is made.
* const { data: lists } = useSuspenseQuery('SELECT * from lists');
*
* return
* {lists.map((l) => (
* {JSON.stringify(l)}
* ))}
* ;
* }
*
* export const DisplayComponent = () => {
* return (
* Loading content...}>
*
*
* );
* }
*
* export const DiffContentComponent = () => {
* // A differential query will emit results when a change to the result set occurs.
* // The internal array object references are maintained for unchanged rows.
* // The returned lists array is read only when a `rowComparator` is provided.
* const { data: lists } = useSuspenseQuery('SELECT * from lists', [], {
* rowComparator: {
* keyBy: (item) => item.id,
* compareBy: (item) => JSON.stringify(item)
* }
* });
* return
* {lists.map((l) => (
* {JSON.stringify(l)}
* ))}
* ;
* }
*
* export const DisplayComponent = () => {
* return (
* Loading content...}>
*
*
* );
* }
*/
export function useSuspenseQuery(
query: string | CompilableQuery,
parameters?: any[],
options?: AdditionalOptions
): SuspenseQueryResult;
export function useSuspenseQuery(
query: string | CompilableQuery,
paramerers?: any[],
options?: DifferentialHookOptions
): ReadonlySuspenseQueryResult;
export function useSuspenseQuery(
query: string | CompilableQuery,
parameters: any[] = [],
options: AdditionalOptions & DifferentialHookOptions = {}
) {
switch (options?.runQueryOnce) {
case true:
return useSingleSuspenseQuery(query, parameters, options);
default:
return useWatchedSuspenseQuery(query, parameters, options);
}
}