import type { UntypedClientTable } from '../sdk/client_table.ts'; import { getByAccessorPath } from '../lib/accessor_path.ts'; import { useCallback, useEffect, useRef, useState, useSyncExternalStore, } from 'react'; import { useSpacetimeDB } from './useSpacetimeDB.ts'; import { type EventContextInterface } from '../sdk/db_connection_impl.ts'; import type { ConnectionState } from './connection_state.ts'; import type { UntypedRemoteModule } from '../sdk/spacetime_module.ts'; import type { RowType, UntypedTableDef } from '../lib/table.ts'; import type { Prettify } from '../lib/type_util.ts'; import { type Query, toSql, type BooleanExpr, evaluateBooleanExpr, getQueryAccessorName, getQueryWhereClause, } from '../lib/query.ts'; export interface UseTableCallbacks { onInsert?: (row: RowType) => void; onDelete?: (row: RowType) => void; onUpdate?: (oldRow: RowType, newRow: RowType) => void; /** Whether the subscription is active. Defaults to `true`. */ enabled?: boolean; } type MembershipChange = 'enter' | 'leave' | 'stayIn' | 'stayOut'; function classifyMembership( whereExpr: BooleanExpr | undefined, oldRow: Record, newRow: Record ): MembershipChange { if (!whereExpr) return 'stayIn'; const oldIn = evaluateBooleanExpr(whereExpr, oldRow); const newIn = evaluateBooleanExpr(whereExpr, newRow); if (oldIn && !newIn) return 'leave'; if (!oldIn && newIn) return 'enter'; if (oldIn && newIn) return 'stayIn'; return 'stayOut'; } /** * React hook to subscribe to a table in SpacetimeDB and receive live updates. * * Accepts a query builder expression as the first argument: * - `tables.user` — subscribe to all rows * - `tables.user.where(r => r.online.eq(true))` — subscribe with a filter * * @param query - A query builder expression (table reference or filtered query). * @param callbacks - Optional callbacks for row insert, delete, and update events. * @returns A tuple of [rows, isReady]. * * @example * ```tsx * const [rows, isReady] = useTable(tables.user); * const [onlineUsers, isReady] = useTable( * tables.user.where(r => r.online.eq(true)), * { onInsert: (row) => console.log('New user:', row) } * ); * ``` */ export function useTable( query: Query, callbacks?: UseTableCallbacks>> ): [readonly Prettify>[], boolean] { type UseTableRowType = RowType; const enabled = callbacks?.enabled ?? true; const accessorName = getQueryAccessorName(query); const whereExpr = getQueryWhereClause(query); const [appliedConnectionId, setAppliedConnectionId] = useState( null ); let connectionState: ConnectionState | undefined; try { connectionState = useSpacetimeDB(); } catch { throw new Error( 'Could not find SpacetimeDB client! Did you forget to add a ' + '`SpacetimeDBProvider`? `useTable` must be used in the React component tree ' + 'under a `SpacetimeDBProvider` component.' ); } const querySql = toSql(query); const connection = connectionState.getConnection(); const latestTransactionEventId = useRef(null); const lastSnapshotRef = useRef< [readonly Prettify[], boolean] | null >(null); const computeSnapshot = useCallback((): [ readonly Prettify[], boolean, ] => { if (!enabled) { return [[], true]; } if (!connection) { return [[], false]; } const table = getByAccessorPath( connection.db, accessorName ); const result: readonly Prettify[] = whereExpr ? (Array.from(table.iter()).filter(row => evaluateBooleanExpr(whereExpr, row as Record) ) as Prettify[]) : (Array.from(table.iter()) as Prettify[]); return [ result, connectionState.isActive && appliedConnectionId === connectionState.connectionId.toHexString(), ]; // TODO: investigating refactoring so that this is no longer necessary, as we have had genuine bugs with missed deps. // See https://github.com/clockworklabs/SpacetimeDB/pull/4580. // eslint-disable-next-line react-hooks/exhaustive-deps }, [ connection, connectionState, accessorName, querySql, appliedConnectionId, enabled, ]); const lastComputeSnapshotRef = useRef(computeSnapshot); useEffect(() => { setAppliedConnectionId(null); if (!enabled || !connection) return; const handle = connection .subscriptionBuilder() .onApplied(() => { setAppliedConnectionId(connection.connectionId.toHexString()); }) .onError(() => setAppliedConnectionId(null)) .subscribe(querySql); return () => handle.unsubscribe(); }, [querySql, connection, enabled]); const subscribe = useCallback( (onStoreChange: () => void) => { if (!enabled) { return () => {}; } const onInsert = ( ctx: EventContextInterface, row: any ) => { if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) { return; } callbacks?.onInsert?.(row); if (ctx.event.id !== latestTransactionEventId.current) { latestTransactionEventId.current = ctx.event.id; lastSnapshotRef.current = computeSnapshot(); onStoreChange(); } }; const onDelete = ( ctx: EventContextInterface, row: any ) => { if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) { return; } callbacks?.onDelete?.(row); if (ctx.event.id !== latestTransactionEventId.current) { latestTransactionEventId.current = ctx.event.id; lastSnapshotRef.current = computeSnapshot(); onStoreChange(); } }; const onUpdate = ( ctx: EventContextInterface, oldRow: any, newRow: any ) => { const change = classifyMembership(whereExpr, oldRow, newRow); switch (change) { case 'leave': callbacks?.onDelete?.(oldRow); break; case 'enter': callbacks?.onInsert?.(newRow); break; case 'stayIn': callbacks?.onUpdate?.(oldRow, newRow); break; case 'stayOut': return; // no-op } if (ctx.event.id !== latestTransactionEventId.current) { latestTransactionEventId.current = ctx.event.id; lastSnapshotRef.current = computeSnapshot(); onStoreChange(); } }; if (!connection) { return () => {}; } const table = getByAccessorPath( connection.db, accessorName ); table.onInsert(onInsert); table.onDelete(onDelete); table.onUpdate?.(onUpdate); return () => { table.removeOnInsert(onInsert); table.removeOnDelete(onDelete); table.removeOnUpdate?.(onUpdate); }; }, // TODO: investigating refactoring so that this is no longer necessary, as we have had genuine bugs with missed deps. // See https://github.com/clockworklabs/SpacetimeDB/pull/4580. // eslint-disable-next-line react-hooks/exhaustive-deps [ connection, accessorName, querySql, computeSnapshot, callbacks?.onDelete, callbacks?.onInsert, callbacks?.onUpdate, enabled, ] ); const getSnapshot = useCallback((): [ readonly Prettify[], boolean, ] => { if (lastComputeSnapshotRef.current !== computeSnapshot) { lastComputeSnapshotRef.current = computeSnapshot; lastSnapshotRef.current = null; } if (!lastSnapshotRef.current) { lastSnapshotRef.current = computeSnapshot(); } return lastSnapshotRef.current; }, [computeSnapshot]); // SSR fallback can be the same getter return useSyncExternalStore(subscribe, getSnapshot, getSnapshot); }