import type { UntypedClientTable } from '../../sdk/client_table.ts'; import { getByAccessorPath } from '../../lib/accessor_path.ts'; import { assertInInjectionContext, inject, signal, computed, effect, type Signal, } from '@angular/core'; import type { RowType, UntypedTableDef } from '../../lib/table.ts'; import type { Prettify } from '../../lib/type_util.ts'; import { SPACETIMEDB_CONNECTION } from '../connection_state.ts'; import { type Query, type BooleanExpr, toSql, evaluateBooleanExpr, getQueryAccessorName, getQueryWhereClause, } from '../../lib/query.ts'; import type { EventContextInterface } from '../../sdk/index.ts'; import type { UntypedRemoteModule } from '../../sdk/spacetime_module.ts'; export type RowTypeDef = Prettify< RowType >; export interface TableRows { rows: readonly RowTypeDef[]; isLoading: boolean; } export interface InjectTableCallbacks { onInsert?: (row: RowType) => void; onDelete?: (row: RowType) => void; onUpdate?: (oldRow: RowType, newRow: RowType) => void; } 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'; } /** * Angular injection function to subscribe to a table in SpacetimeDB and receive live updates. * * Must be called within an injection context (component field initializer or constructor). * * 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 * * @template TableDef The table definition type. * * @param query - A query builder expression (table reference or filtered query). * @param callbacks - Optional callbacks for row insert, delete, and update events. * * @returns A signal containing the current rows and loading state. * * @example * ```typescript * export class UsersComponent { * users = injectTable(tables.user); * * // With a filter: * onlineUsers = injectTable( * tables.user.where(r => r.online.eq(true)), * { * onInsert: (row) => console.log('Inserted:', row), * onDelete: (row) => console.log('Deleted:', row), * onUpdate: (oldRow, newRow) => console.log('Updated:', oldRow, newRow), * } * ); * * // In template: {{ users().rows.length }} users * // Loading state: {{ users().isLoading }} * } * ``` */ export function injectTable( query: Query, callbacks?: InjectTableCallbacks> ): Signal> { assertInInjectionContext(injectTable); const connState = inject(SPACETIMEDB_CONNECTION); const accessorName = getQueryAccessorName(query); const whereExpr = getQueryWhereClause(query); const querySql = toSql(query); const rows = signal[]>([]); let latestTransactionEvent: any = null; const appliedConnectionId = signal(null); const connection = computed(() => connState().getConnection()); // Note: this code is mostly derived from the React useTable implementation // in order to keep behavior consistent across frameworks. const computeSnapshot = (): readonly RowTypeDef[] => { const currentConnection = connection(); if (!currentConnection) { return []; } const table = getByAccessorPath( currentConnection.db, accessorName ); if (whereExpr) { return Array.from(table.iter()).filter(row => evaluateBooleanExpr(whereExpr, row as Record) ) as RowTypeDef[]; } return Array.from(table.iter()) as RowTypeDef[]; }; const updateSnapshot = () => { rows.set(computeSnapshot()); }; effect((onCleanup: (fn: () => void) => void) => { appliedConnectionId.set(null); const currentConnection = connection(); if (!currentConnection) { updateSnapshot(); return; } const table = getByAccessorPath( currentConnection.db, accessorName ); const onInsert = ( ctx: EventContextInterface, row: any ) => { if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) { return; } callbacks?.onInsert?.(row); if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) { latestTransactionEvent = ctx.event; updateSnapshot(); } }; const onDelete = ( ctx: EventContextInterface, row: any ) => { if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) { return; } callbacks?.onDelete?.(row); if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) { latestTransactionEvent = ctx.event; updateSnapshot(); } }; 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; } if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) { latestTransactionEvent = ctx.event; updateSnapshot(); } }; table.onInsert(onInsert); table.onDelete(onDelete); table.onUpdate?.(onUpdate); const subscription = currentConnection .subscriptionBuilder() .onApplied(() => { appliedConnectionId.set(currentConnection.connectionId.toHexString()); updateSnapshot(); }) .onError(() => appliedConnectionId.set(null)) .subscribe(querySql); updateSnapshot(); onCleanup(() => { table.removeOnInsert(onInsert); table.removeOnDelete(onDelete); table.removeOnUpdate?.(onUpdate); subscription.unsubscribe(); }); }); return computed(() => ({ rows: rows(), isLoading: !connState().isActive || appliedConnectionId() !== connState().connectionId.toHexString(), })); }