spacetimedb 2.4.1 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,97 @@
1
+ import {
2
+ DbConnectionBuilder,
3
+ type DbConnectionImpl,
4
+ } from '../sdk/db_connection_impl';
5
+ import { onCleanup, createMemo, createComputed } from 'solid-js';
6
+ import { createStore } from 'solid-js/store';
7
+ import { SpacetimeDBContext } from './useSpacetimeDB';
8
+ import type { ConnectionState } from './connection_state';
9
+ import { ConnectionId } from '../lib/connection_id';
10
+ import {
11
+ ConnectionManager,
12
+ type ConnectionState as ManagerConnectionState,
13
+ } from '../sdk/connection_manager';
14
+
15
+ export interface SpacetimeDBProviderProps<
16
+ DbConnection extends DbConnectionImpl<any>,
17
+ > {
18
+ connectionBuilder: DbConnectionBuilder<DbConnection>;
19
+ children?: any;
20
+ }
21
+
22
+ export function SpacetimeDBProvider<DbConnection extends DbConnectionImpl<any>>(
23
+ props: SpacetimeDBProviderProps<DbConnection>
24
+ ) {
25
+ const uri = () => props.connectionBuilder.getUri();
26
+ const moduleName = () => props.connectionBuilder.getModuleName();
27
+
28
+ const key = createMemo(() => ConnectionManager.getKey(uri(), moduleName()));
29
+
30
+ const fallbackState: ManagerConnectionState = {
31
+ isActive: false,
32
+ identity: undefined,
33
+ token: undefined,
34
+ connectionId: ConnectionId.random(),
35
+ connectionError: undefined,
36
+ };
37
+
38
+ const [state, setState] = createStore<ManagerConnectionState>(fallbackState);
39
+
40
+ // Subscribe to ConnectionManager state changes
41
+ createComputed(() => {
42
+ const currentKey = key();
43
+
44
+ const unsubscribe = ConnectionManager.subscribe(currentKey, () => {
45
+ const snapshot =
46
+ ConnectionManager.getSnapshot(currentKey) ?? fallbackState;
47
+ setState(snapshot);
48
+ });
49
+
50
+ // Load initial snapshot
51
+ const snapshot = ConnectionManager.getSnapshot(currentKey) ?? fallbackState;
52
+ setState(snapshot);
53
+
54
+ onCleanup(() => {
55
+ unsubscribe();
56
+ });
57
+ });
58
+
59
+ const getConnection = () =>
60
+ ConnectionManager.getConnection<DbConnection>(key());
61
+
62
+ const contextValue: ConnectionState = {
63
+ get isActive() {
64
+ return state.isActive;
65
+ },
66
+ get identity() {
67
+ return state.identity;
68
+ },
69
+ get token() {
70
+ return state.token;
71
+ },
72
+ get connectionId() {
73
+ return state.connectionId;
74
+ },
75
+ get connectionError() {
76
+ return state.connectionError;
77
+ },
78
+ getConnection,
79
+ };
80
+
81
+ // Retain / release lifecycle
82
+ createComputed(() => {
83
+ const currentKey = key();
84
+ ConnectionManager.retain(currentKey, props.connectionBuilder);
85
+
86
+ onCleanup(() => {
87
+ ConnectionManager.release(currentKey);
88
+ });
89
+ });
90
+
91
+ return SpacetimeDBContext.Provider({
92
+ value: contextValue,
93
+ get children() {
94
+ return props.children;
95
+ },
96
+ });
97
+ }
@@ -0,0 +1,6 @@
1
+ import type { DbConnectionImpl } from '../sdk/db_connection_impl';
2
+ import type { ConnectionState as ManagerConnectionState } from '../sdk/connection_manager';
3
+
4
+ export type ConnectionState = ManagerConnectionState & {
5
+ getConnection(): DbConnectionImpl<any> | null;
6
+ };
@@ -0,0 +1,5 @@
1
+ export * from './SpacetimeDBProvider.ts';
2
+ export { useSpacetimeDB } from './useSpacetimeDB.ts';
3
+ export { useTable } from './useTable.ts';
4
+ export { useReducer } from './useReducer.ts';
5
+ export { useProcedure } from './useProcedure.ts';
@@ -0,0 +1,57 @@
1
+ import { createEffect } from 'solid-js';
2
+ import type { UntypedProcedureDef } from '../sdk/procedures';
3
+ import { useSpacetimeDB } from './useSpacetimeDB';
4
+ import type {
5
+ ProcedureParamsType,
6
+ ProcedureReturnType,
7
+ } from '../sdk/type_utils';
8
+
9
+ export function useProcedure<ProcedureDef extends UntypedProcedureDef>(
10
+ procedureDef: ProcedureDef
11
+ ): (
12
+ ...params: ProcedureParamsType<ProcedureDef>
13
+ ) => Promise<ProcedureReturnType<ProcedureDef>> {
14
+ const { getConnection, isActive } = useSpacetimeDB();
15
+ const procedureName = procedureDef.accessorName;
16
+
17
+ // Holds calls made before the connection exists
18
+ const queue: {
19
+ params: ProcedureParamsType<ProcedureDef>;
20
+ resolve: (val: any) => void;
21
+ reject: (err: unknown) => void;
22
+ }[] = [];
23
+
24
+ // Flush when we finally have a connection
25
+ createEffect(() => {
26
+ if (!isActive) return;
27
+
28
+ const conn = getConnection();
29
+ if (!conn) return;
30
+
31
+ const fn = (conn.procedures as any)[procedureName] as (
32
+ ...p: ProcedureParamsType<ProcedureDef>
33
+ ) => Promise<ProcedureReturnType<ProcedureDef>>;
34
+
35
+ if (queue.length) {
36
+ const pending = queue.splice(0);
37
+ for (const item of pending) {
38
+ fn(...item.params).then(item.resolve, item.reject);
39
+ }
40
+ }
41
+ });
42
+
43
+ return (...params: ProcedureParamsType<ProcedureDef>) => {
44
+ const conn = getConnection();
45
+ if (!conn) {
46
+ return new Promise<ProcedureReturnType<ProcedureDef>>(
47
+ (resolve, reject) => {
48
+ queue.push({ params, resolve, reject });
49
+ }
50
+ );
51
+ }
52
+ const fn = (conn.procedures as any)[procedureName] as (
53
+ ...p: ProcedureParamsType<ProcedureDef>
54
+ ) => Promise<ProcedureReturnType<ProcedureDef>>;
55
+ return fn(...params);
56
+ };
57
+ }
@@ -0,0 +1,50 @@
1
+ import { createEffect } from 'solid-js';
2
+ import type { UntypedReducerDef } from '../sdk/reducers';
3
+ import { useSpacetimeDB } from './useSpacetimeDB';
4
+ import type { ParamsType } from '../sdk';
5
+
6
+ export function useReducer<ReducerDef extends UntypedReducerDef>(
7
+ reducerDef: ReducerDef
8
+ ): (...params: ParamsType<ReducerDef>) => Promise<void> {
9
+ const { getConnection, isActive } = useSpacetimeDB();
10
+ const reducerName = reducerDef.accessorName;
11
+
12
+ // Holds calls made before the connection exists
13
+ const queue: {
14
+ params: ParamsType<ReducerDef>;
15
+ resolve: () => void;
16
+ reject: (err: unknown) => void;
17
+ }[] = [];
18
+
19
+ // Flush when we finally have a connection
20
+ createEffect(() => {
21
+ if (!isActive) return;
22
+
23
+ const conn = getConnection();
24
+ if (!conn) return;
25
+
26
+ const fn = (conn.reducers as any)[reducerName] as (
27
+ ...p: ParamsType<ReducerDef>
28
+ ) => Promise<void>;
29
+
30
+ if (queue.length) {
31
+ const pending = queue.splice(0);
32
+ for (const item of pending) {
33
+ fn(...item.params).then(item.resolve, item.reject);
34
+ }
35
+ }
36
+ });
37
+
38
+ return (...params: ParamsType<ReducerDef>) => {
39
+ const conn = getConnection();
40
+ if (!conn) {
41
+ return new Promise<void>((resolve, reject) => {
42
+ queue.push({ params, resolve, reject });
43
+ });
44
+ }
45
+ const fn = (conn.reducers as any)[reducerName] as (
46
+ ...p: ParamsType<ReducerDef>
47
+ ) => Promise<void>;
48
+ return fn(...params);
49
+ };
50
+ }
@@ -0,0 +1,18 @@
1
+ import { createContext, useContext } from 'solid-js';
2
+ import type { ConnectionState } from './connection_state';
3
+
4
+ export const SpacetimeDBContext = createContext<ConnectionState | undefined>(
5
+ undefined
6
+ );
7
+
8
+ // Throws an error if used outside of a SpacetimeDBProvider
9
+ // Error is caught by other hooks like useTable so they can provide better error messages
10
+ export function useSpacetimeDB(): ConnectionState {
11
+ const context = useContext(SpacetimeDBContext) as ConnectionState | undefined;
12
+ if (!context) {
13
+ throw new Error(
14
+ 'useSpacetimeDB must be used within a SpacetimeDBProvider component. Did you forget to add a `SpacetimeDBProvider` to your component tree?'
15
+ );
16
+ }
17
+ return context;
18
+ }
@@ -0,0 +1,203 @@
1
+ import { createSignal, onCleanup, createMemo, createComputed } from 'solid-js';
2
+ import { useSpacetimeDB } from './useSpacetimeDB';
3
+ import { type EventContextInterface } from '../sdk/db_connection_impl';
4
+ import type { UntypedRemoteModule } from '../sdk/spacetime_module';
5
+ import type { RowType, UntypedTableDef } from '../lib/table';
6
+ import type { Prettify } from '../lib/type_util';
7
+ import {
8
+ type Query,
9
+ type BooleanExpr,
10
+ toSql,
11
+ evaluateBooleanExpr,
12
+ getQueryAccessorName,
13
+ getQueryWhereClause,
14
+ } from '../lib/query';
15
+ import { createStore, reconcile } from 'solid-js/store';
16
+
17
+ export interface UseTableCallbacks<RowType> {
18
+ onInsert?: (row: RowType) => void;
19
+ onDelete?: (row: RowType) => void;
20
+ onUpdate?: (oldRow: RowType, newRow: RowType) => void;
21
+ /** Whether the subscription is active. Defaults to `true`. */
22
+ enabled?: () => boolean;
23
+ }
24
+
25
+ type MembershipChange = 'enter' | 'leave' | 'stayIn' | 'stayOut';
26
+
27
+ function classifyMembership(
28
+ whereExpr: BooleanExpr<any> | undefined,
29
+ oldRow: Record<string, any>,
30
+ newRow: Record<string, any>
31
+ ): MembershipChange {
32
+ if (!whereExpr) return 'stayIn';
33
+ const oldIn = evaluateBooleanExpr(whereExpr, oldRow);
34
+ const newIn = evaluateBooleanExpr(whereExpr, newRow);
35
+ if (oldIn && !newIn) return 'leave';
36
+ if (!oldIn && newIn) return 'enter';
37
+ if (oldIn && newIn) return 'stayIn';
38
+ return 'stayOut';
39
+ }
40
+
41
+ /**
42
+ * SolidJS primitive to subscribe to a table in SpacetimeDB and receive live updates.
43
+ *
44
+ * Accepts a query builder expression as the first argument:
45
+ * - `tables.user` — subscribe to all rows
46
+ * - `tables.user.where(r => r.online.eq(true))` — subscribe with a filter
47
+ *
48
+ * @param query - A query builder expression (table reference or filtered query).
49
+ * @param callbacks - Optional callbacks for row insert, delete, and update events.
50
+ * @returns A tuple of [rows, isReady].
51
+ *
52
+ * @example
53
+ * ```tsx
54
+ * const [rows, isReady] = useTable(tables.user);
55
+ * const [onlineUsers, isReady] = useTable(
56
+ * tables.user.where(r => r.online.eq(true)),
57
+ * { onInsert: (row) => console.log('New user:', row) }
58
+ * );
59
+ * ```
60
+ */
61
+ export function useTable<TableDef extends UntypedTableDef>(
62
+ query: () => Query<TableDef>,
63
+ callbacks?: UseTableCallbacks<Prettify<RowType<TableDef>>>
64
+ ): [readonly Prettify<RowType<TableDef>>[], () => boolean] {
65
+ type UseTableRowType = RowType<TableDef>;
66
+ const enabled = callbacks?.enabled ?? (() => true);
67
+ const q = createMemo(query);
68
+ const accessorName = createMemo(() => getQueryAccessorName(q()));
69
+ const whereExpr = createMemo(() => getQueryWhereClause(q()));
70
+ const querySql = createMemo(() => toSql(q()));
71
+
72
+ let connectionState: import('./connection_state').ConnectionState;
73
+ try {
74
+ connectionState = useSpacetimeDB();
75
+ } catch {
76
+ throw new Error(
77
+ 'Could not find SpacetimeDB client! Did you forget to add a ' +
78
+ '`SpacetimeDBProvider`? `useTable` must be used in the SolidJS component tree ' +
79
+ 'under a `SpacetimeDBProvider` component.'
80
+ );
81
+ }
82
+
83
+ const [rows, setRows] = createStore<readonly Prettify<UseTableRowType>[]>([]);
84
+
85
+ let latestTransactionEventId: string | null = null;
86
+
87
+ const computeSnapshot = (): readonly Prettify<UseTableRowType>[] => {
88
+ if (!enabled()) {
89
+ return [];
90
+ }
91
+
92
+ const connection = connectionState.getConnection();
93
+ if (!connection) {
94
+ return [];
95
+ }
96
+ const table = connection.db[accessorName()];
97
+ const result: readonly Prettify<UseTableRowType>[] = whereExpr()
98
+ ? (Array.from(table.iter()).filter(row =>
99
+ evaluateBooleanExpr(whereExpr()!, row as Record<string, any>)
100
+ ) as Prettify<UseTableRowType>[])
101
+ : (Array.from(table.iter()) as Prettify<UseTableRowType>[]);
102
+ return result;
103
+ };
104
+
105
+ const [isReady, setIsReady] = createSignal(false);
106
+
107
+ createComputed(() => {
108
+ if (!enabled()) {
109
+ setIsReady(false);
110
+ return;
111
+ }
112
+
113
+ const connection = connectionState.getConnection();
114
+ if (!connectionState.isActive || !connection) {
115
+ setIsReady(false);
116
+ return;
117
+ }
118
+
119
+ const cancel = connection
120
+ .subscriptionBuilder()
121
+ .onApplied(() => {
122
+ setIsReady(true);
123
+ })
124
+ .subscribe(querySql());
125
+
126
+ onCleanup(() => {
127
+ cancel.unsubscribe();
128
+ });
129
+
130
+ // Bind to table events
131
+ const table = connection.db[accessorName()];
132
+
133
+ const onInsert = (
134
+ ctx: EventContextInterface<UntypedRemoteModule>,
135
+ row: any
136
+ ) => {
137
+ if (whereExpr() && !evaluateBooleanExpr(whereExpr()!, row)) {
138
+ return;
139
+ }
140
+ callbacks?.onInsert?.(row);
141
+ if (ctx.event.id !== latestTransactionEventId) {
142
+ latestTransactionEventId = ctx.event.id;
143
+ setRows(reconcile(computeSnapshot()));
144
+ }
145
+ };
146
+
147
+ const onDelete = (
148
+ ctx: EventContextInterface<UntypedRemoteModule>,
149
+ row: any
150
+ ) => {
151
+ if (whereExpr() && !evaluateBooleanExpr(whereExpr()!, row)) {
152
+ return;
153
+ }
154
+ callbacks?.onDelete?.(row);
155
+ if (ctx.event.id !== latestTransactionEventId) {
156
+ latestTransactionEventId = ctx.event.id;
157
+ setRows(reconcile(computeSnapshot()));
158
+ }
159
+ };
160
+
161
+ const onUpdate = (
162
+ ctx: EventContextInterface<UntypedRemoteModule>,
163
+ oldRow: any,
164
+ newRow: any
165
+ ) => {
166
+ const change = classifyMembership(whereExpr(), oldRow, newRow);
167
+
168
+ switch (change) {
169
+ case 'leave':
170
+ callbacks?.onDelete?.(oldRow);
171
+ break;
172
+ case 'enter':
173
+ callbacks?.onInsert?.(newRow);
174
+ break;
175
+ case 'stayIn':
176
+ callbacks?.onUpdate?.(oldRow, newRow);
177
+ break;
178
+ case 'stayOut':
179
+ return; // no-op
180
+ }
181
+
182
+ if (ctx.event.id !== latestTransactionEventId) {
183
+ latestTransactionEventId = ctx.event.id;
184
+ setRows(reconcile(computeSnapshot()));
185
+ }
186
+ };
187
+
188
+ table.onInsert(onInsert);
189
+ table.onDelete(onDelete);
190
+ table.onUpdate?.(onUpdate);
191
+
192
+ // Load initial snapshot
193
+ setRows(reconcile(computeSnapshot()));
194
+
195
+ onCleanup(() => {
196
+ table.removeOnInsert(onInsert);
197
+ table.removeOnDelete(onDelete);
198
+ table.removeOnUpdate?.(onUpdate);
199
+ });
200
+ });
201
+
202
+ return [rows, isReady];
203
+ }