spacetimedb 2.4.1 → 2.6.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.
- package/LICENSE.txt +759 -759
- package/README.md +211 -120
- package/dist/angular/index.cjs.map +1 -1
- package/dist/angular/index.mjs.map +1 -1
- package/dist/browser/angular/index.mjs.map +1 -1
- package/dist/browser/react/index.mjs +129 -57
- package/dist/browser/react/index.mjs.map +1 -1
- package/dist/browser/solid/index.mjs +1933 -0
- package/dist/browser/solid/index.mjs.map +1 -0
- package/dist/browser/svelte/index.mjs.map +1 -1
- package/dist/browser/vue/index.mjs.map +1 -1
- package/dist/index.browser.mjs +10 -2
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.cjs +10 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +10 -2
- package/dist/index.mjs.map +1 -1
- package/dist/min/index.browser.mjs +1 -1
- package/dist/min/index.browser.mjs.map +1 -1
- package/dist/min/react/index.mjs +1 -1
- package/dist/min/react/index.mjs.map +1 -1
- package/dist/min/sdk/index.browser.mjs +1 -1
- package/dist/min/sdk/index.browser.mjs.map +1 -1
- package/dist/react/index.cjs +129 -57
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.mjs +129 -57
- package/dist/react/index.mjs.map +1 -1
- package/dist/react/useTable.d.ts.map +1 -1
- package/dist/sdk/connection_manager.d.ts +8 -0
- package/dist/sdk/connection_manager.d.ts.map +1 -1
- package/dist/sdk/db_connection_impl.d.ts +7 -0
- package/dist/sdk/db_connection_impl.d.ts.map +1 -1
- package/dist/sdk/index.browser.mjs +10 -2
- package/dist/sdk/index.browser.mjs.map +1 -1
- package/dist/sdk/index.cjs +10 -2
- package/dist/sdk/index.cjs.map +1 -1
- package/dist/sdk/index.mjs +10 -2
- package/dist/sdk/index.mjs.map +1 -1
- package/dist/sdk/websocket_test_adapter.d.ts +2 -1
- package/dist/sdk/websocket_test_adapter.d.ts.map +1 -1
- package/dist/server/index.mjs.map +1 -1
- package/dist/server/runtime.d.ts.map +1 -1
- package/dist/solid/SpacetimeDBProvider.d.ts +7 -0
- package/dist/solid/SpacetimeDBProvider.d.ts.map +1 -0
- package/dist/solid/connection_state.d.ts +6 -0
- package/dist/solid/connection_state.d.ts.map +1 -0
- package/dist/solid/index.cjs +1939 -0
- package/dist/solid/index.cjs.map +1 -0
- package/dist/solid/index.d.ts +6 -0
- package/dist/solid/index.d.ts.map +1 -0
- package/dist/solid/index.mjs +1933 -0
- package/dist/solid/index.mjs.map +1 -0
- package/dist/solid/useProcedure.d.ts +4 -0
- package/dist/solid/useProcedure.d.ts.map +1 -0
- package/dist/solid/useReducer.d.ts +4 -0
- package/dist/solid/useReducer.d.ts.map +1 -0
- package/dist/solid/useSpacetimeDB.d.ts +4 -0
- package/dist/solid/useSpacetimeDB.d.ts.map +1 -0
- package/dist/solid/useTable.d.ts +32 -0
- package/dist/solid/useTable.d.ts.map +1 -0
- package/dist/svelte/index.cjs.map +1 -1
- package/dist/svelte/index.mjs.map +1 -1
- package/dist/tanstack/index.cjs +120 -50
- package/dist/tanstack/index.cjs.map +1 -1
- package/dist/tanstack/index.mjs +120 -50
- package/dist/tanstack/index.mjs.map +1 -1
- package/dist/vue/index.cjs.map +1 -1
- package/dist/vue/index.mjs.map +1 -1
- package/package.json +13 -3
- package/src/angular/connection_state.ts +19 -19
- package/src/angular/index.ts +3 -3
- package/src/angular/injectors/index.ts +4 -4
- package/src/angular/injectors/inject-reducer.ts +62 -62
- package/src/angular/injectors/inject-spacetimedb-connected.ts +13 -13
- package/src/angular/injectors/inject-spacetimedb.ts +10 -10
- package/src/angular/injectors/inject-table.ts +234 -234
- package/src/angular/providers/index.ts +1 -1
- package/src/angular/providers/provide-spacetimedb.ts +96 -96
- package/src/index.ts +16 -16
- package/src/lib/algebraic_type.ts +819 -819
- package/src/lib/algebraic_type_variants.ts +26 -26
- package/src/lib/algebraic_value.ts +10 -10
- package/src/lib/autogen/types.ts +746 -746
- package/src/lib/binary_reader.ts +188 -188
- package/src/lib/binary_writer.ts +213 -213
- package/src/lib/connection_id.ts +102 -102
- package/src/lib/constraints.ts +48 -48
- package/src/lib/errors.ts +26 -26
- package/src/lib/filter.ts +195 -195
- package/src/lib/identity.ts +83 -83
- package/src/lib/indexes.ts +251 -251
- package/src/lib/option.ts +34 -34
- package/src/lib/query.ts +1019 -1019
- package/src/lib/reducer_schema.ts +38 -38
- package/src/lib/reducers.ts +116 -116
- package/src/lib/result.ts +36 -36
- package/src/lib/schedule_at.ts +86 -86
- package/src/lib/schema.ts +420 -420
- package/src/lib/table.ts +548 -548
- package/src/lib/table_schema.ts +64 -64
- package/src/lib/time_duration.ts +77 -77
- package/src/lib/timestamp.ts +148 -148
- package/src/lib/type_builders.test-d.ts +128 -128
- package/src/lib/type_builders.ts +4014 -4014
- package/src/lib/type_util.ts +124 -124
- package/src/lib/util.ts +196 -196
- package/src/lib/uuid.ts +337 -337
- package/src/react/SpacetimeDBProvider.ts +84 -84
- package/src/react/connection_state.ts +6 -6
- package/src/react/index.ts +5 -5
- package/src/react/useProcedure.ts +60 -60
- package/src/react/useReducer.ts +53 -53
- package/src/react/useSpacetimeDB.ts +18 -18
- package/src/react/useTable.ts +256 -251
- package/src/sdk/client_api/index.ts +114 -114
- package/src/sdk/client_api/types/procedures.ts +8 -8
- package/src/sdk/client_api/types/reducers.ts +8 -8
- package/src/sdk/client_api/types.ts +288 -288
- package/src/sdk/client_cache.ts +129 -129
- package/src/sdk/client_table.ts +179 -179
- package/src/sdk/connection_manager.ts +352 -237
- package/src/sdk/db_connection_builder.ts +290 -290
- package/src/sdk/db_connection_impl.ts +1356 -1347
- package/src/sdk/db_context.ts +28 -28
- package/src/sdk/db_view.ts +12 -12
- package/src/sdk/decompress.ts +51 -51
- package/src/sdk/event.ts +18 -18
- package/src/sdk/event_context.ts +51 -51
- package/src/sdk/event_emitter.ts +32 -32
- package/src/sdk/index.ts +14 -14
- package/src/sdk/internal.ts +2 -2
- package/src/sdk/json_api.ts +46 -46
- package/src/sdk/logger.ts +134 -134
- package/src/sdk/message_types.ts +46 -46
- package/src/sdk/procedures.ts +83 -83
- package/src/sdk/reducer_event.ts +20 -20
- package/src/sdk/reducer_handle.ts +12 -12
- package/src/sdk/reducers.ts +159 -159
- package/src/sdk/schema.ts +45 -45
- package/src/sdk/spacetime_module.ts +28 -28
- package/src/sdk/subscription_builder_impl.ts +275 -275
- package/src/sdk/table_cache.ts +581 -581
- package/src/sdk/type_utils.ts +19 -19
- package/src/sdk/version.ts +133 -133
- package/src/sdk/websocket_decompress_adapter.ts +63 -63
- package/src/sdk/websocket_protocols.ts +25 -25
- package/src/sdk/websocket_test_adapter.ts +107 -100
- package/src/sdk/websocket_v3_frames.ts +126 -126
- package/src/sdk/ws.ts +105 -105
- package/src/server/console.ts +81 -81
- package/src/server/db_view.ts +21 -21
- package/src/server/errors.ts +138 -138
- package/src/server/http.test-d.ts +80 -80
- package/src/server/http.ts +14 -14
- package/src/server/http_handlers.ts +413 -413
- package/src/server/http_internal.ts +79 -79
- package/src/server/http_shared.ts +186 -186
- package/src/server/index.ts +37 -37
- package/src/server/polyfills.ts +4 -4
- package/src/server/procedures.ts +239 -239
- package/src/server/query.ts +1 -1
- package/src/server/range.ts +53 -53
- package/src/server/reducers.ts +113 -113
- package/src/server/rng.ts +113 -113
- package/src/server/runtime.ts +1102 -1102
- package/src/server/schema.test-d.ts +99 -99
- package/src/server/schema.ts +663 -663
- package/src/server/sys.d.ts +125 -125
- package/src/server/view.test-d.ts +194 -194
- package/src/server/views.ts +340 -340
- package/src/solid/SpacetimeDBProvider.ts +97 -0
- package/src/solid/connection_state.ts +6 -0
- package/src/solid/index.ts +5 -0
- package/src/solid/useProcedure.ts +57 -0
- package/src/solid/useReducer.ts +50 -0
- package/src/solid/useSpacetimeDB.ts +18 -0
- package/src/solid/useTable.ts +203 -0
- package/src/svelte/SpacetimeDBProvider.ts +101 -101
- package/src/svelte/connection_state.ts +16 -16
- package/src/svelte/index.ts +4 -4
- package/src/svelte/useReducer.ts +61 -61
- package/src/svelte/useSpacetimeDB.ts +22 -22
- package/src/svelte/useTable.ts +218 -218
- package/src/tanstack/SpacetimeDBQueryClient.ts +330 -330
- package/src/tanstack/hooks.ts +83 -83
- package/src/tanstack/index.ts +16 -16
- package/src/util-stub.ts +1 -1
- package/src/vue/SpacetimeDBProvider.ts +157 -157
- package/src/vue/connection_state.ts +19 -19
- package/src/vue/index.ts +5 -5
- package/src/vue/useProcedure.ts +62 -62
- package/src/vue/useReducer.ts +55 -55
- package/src/vue/useSpacetimeDB.ts +18 -18
- package/src/vue/useTable.ts +229 -229
package/src/react/useTable.ts
CHANGED
|
@@ -1,251 +1,256 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useCallback,
|
|
3
|
-
useEffect,
|
|
4
|
-
useRef,
|
|
5
|
-
useState,
|
|
6
|
-
useSyncExternalStore,
|
|
7
|
-
} from 'react';
|
|
8
|
-
import { useSpacetimeDB } from './useSpacetimeDB';
|
|
9
|
-
import { type EventContextInterface } from '../sdk/db_connection_impl';
|
|
10
|
-
import type { ConnectionState } from './connection_state';
|
|
11
|
-
import type { UntypedRemoteModule } from '../sdk/spacetime_module';
|
|
12
|
-
import type { RowType, UntypedTableDef } from '../lib/table';
|
|
13
|
-
import type { Prettify } from '../lib/type_util';
|
|
14
|
-
import {
|
|
15
|
-
type Query,
|
|
16
|
-
toSql,
|
|
17
|
-
type BooleanExpr,
|
|
18
|
-
evaluateBooleanExpr,
|
|
19
|
-
getQueryAccessorName,
|
|
20
|
-
getQueryWhereClause,
|
|
21
|
-
} from '../lib/query';
|
|
22
|
-
|
|
23
|
-
export interface UseTableCallbacks<RowType> {
|
|
24
|
-
onInsert?: (row: RowType) => void;
|
|
25
|
-
onDelete?: (row: RowType) => void;
|
|
26
|
-
onUpdate?: (oldRow: RowType, newRow: RowType) => void;
|
|
27
|
-
/** Whether the subscription is active. Defaults to `true`. */
|
|
28
|
-
enabled?: boolean;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
type MembershipChange = 'enter' | 'leave' | 'stayIn' | 'stayOut';
|
|
32
|
-
|
|
33
|
-
function classifyMembership(
|
|
34
|
-
whereExpr: BooleanExpr<any> | undefined,
|
|
35
|
-
oldRow: Record<string, any>,
|
|
36
|
-
newRow: Record<string, any>
|
|
37
|
-
): MembershipChange {
|
|
38
|
-
if (!whereExpr) return 'stayIn';
|
|
39
|
-
const oldIn = evaluateBooleanExpr(whereExpr, oldRow);
|
|
40
|
-
const newIn = evaluateBooleanExpr(whereExpr, newRow);
|
|
41
|
-
if (oldIn && !newIn) return 'leave';
|
|
42
|
-
if (!oldIn && newIn) return 'enter';
|
|
43
|
-
if (oldIn && newIn) return 'stayIn';
|
|
44
|
-
return 'stayOut';
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* React hook to subscribe to a table in SpacetimeDB and receive live updates.
|
|
49
|
-
*
|
|
50
|
-
* Accepts a query builder expression as the first argument:
|
|
51
|
-
* - `tables.user` — subscribe to all rows
|
|
52
|
-
* - `tables.user.where(r => r.online.eq(true))` — subscribe with a filter
|
|
53
|
-
*
|
|
54
|
-
* @param query - A query builder expression (table reference or filtered query).
|
|
55
|
-
* @param callbacks - Optional callbacks for row insert, delete, and update events.
|
|
56
|
-
* @returns A tuple of [rows, isReady].
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```tsx
|
|
60
|
-
* const [rows, isReady] = useTable(tables.user);
|
|
61
|
-
* const [onlineUsers, isReady] = useTable(
|
|
62
|
-
* tables.user.where(r => r.online.eq(true)),
|
|
63
|
-
* { onInsert: (row) => console.log('New user:', row) }
|
|
64
|
-
* );
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export function useTable<TableDef extends UntypedTableDef>(
|
|
68
|
-
query: Query<TableDef>,
|
|
69
|
-
callbacks?: UseTableCallbacks<Prettify<RowType<TableDef>>>
|
|
70
|
-
): [readonly Prettify<RowType<TableDef>>[], boolean] {
|
|
71
|
-
type UseTableRowType = RowType<TableDef>;
|
|
72
|
-
const enabled = callbacks?.enabled ?? true;
|
|
73
|
-
const accessorName = getQueryAccessorName(query);
|
|
74
|
-
const whereExpr = getQueryWhereClause(query);
|
|
75
|
-
|
|
76
|
-
const [subscribeApplied, setSubscribeApplied] = useState(false);
|
|
77
|
-
let connectionState: ConnectionState | undefined;
|
|
78
|
-
try {
|
|
79
|
-
connectionState = useSpacetimeDB();
|
|
80
|
-
} catch {
|
|
81
|
-
throw new Error(
|
|
82
|
-
'Could not find SpacetimeDB client! Did you forget to add a ' +
|
|
83
|
-
'`SpacetimeDBProvider`? `useTable` must be used in the React component tree ' +
|
|
84
|
-
'under a `SpacetimeDBProvider` component.'
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const querySql = toSql(query);
|
|
89
|
-
|
|
90
|
-
const latestTransactionEventId = useRef<string | null>(null);
|
|
91
|
-
const lastSnapshotRef = useRef<
|
|
92
|
-
[readonly Prettify<UseTableRowType>[], boolean] | null
|
|
93
|
-
>(null);
|
|
94
|
-
|
|
95
|
-
const computeSnapshot = useCallback((): [
|
|
96
|
-
readonly Prettify<UseTableRowType>[],
|
|
97
|
-
boolean,
|
|
98
|
-
] => {
|
|
99
|
-
if (!enabled) {
|
|
100
|
-
return [[], true];
|
|
101
|
-
}
|
|
102
|
-
const connection = connectionState.getConnection();
|
|
103
|
-
if (!connection) {
|
|
104
|
-
return [[], false];
|
|
105
|
-
}
|
|
106
|
-
const table = connection.db[accessorName];
|
|
107
|
-
const result: readonly Prettify<UseTableRowType>[] = whereExpr
|
|
108
|
-
? (Array.from(table.iter()).filter(row =>
|
|
109
|
-
evaluateBooleanExpr(whereExpr, row as Record<string, any>)
|
|
110
|
-
) as Prettify<UseTableRowType>[])
|
|
111
|
-
: (Array.from(table.iter()) as Prettify<UseTableRowType>[]);
|
|
112
|
-
return [result, subscribeApplied];
|
|
113
|
-
// TODO: investigating refactoring so that this is no longer necessary, as we have had genuine bugs with missed deps.
|
|
114
|
-
// See https://github.com/clockworklabs/SpacetimeDB/pull/4580.
|
|
115
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
116
|
-
}, [connectionState, accessorName, querySql, subscribeApplied, enabled]);
|
|
117
|
-
|
|
118
|
-
// Invalidate the cached snapshot when computeSnapshot changes (e.g. when
|
|
119
|
-
// subscribeApplied flips to true) so getSnapshot() recomputes on the next
|
|
120
|
-
// render instead of returning a stale [rows, false] tuple.
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
lastSnapshotRef.current = null;
|
|
123
|
-
}, [computeSnapshot]);
|
|
124
|
-
|
|
125
|
-
useEffect(() => {
|
|
126
|
-
if (!enabled) {
|
|
127
|
-
setSubscribeApplied(false);
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
const connection = connectionState.getConnection()
|
|
131
|
-
if (connectionState.isActive
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useState,
|
|
6
|
+
useSyncExternalStore,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import { useSpacetimeDB } from './useSpacetimeDB';
|
|
9
|
+
import { type EventContextInterface } from '../sdk/db_connection_impl';
|
|
10
|
+
import type { ConnectionState } from './connection_state';
|
|
11
|
+
import type { UntypedRemoteModule } from '../sdk/spacetime_module';
|
|
12
|
+
import type { RowType, UntypedTableDef } from '../lib/table';
|
|
13
|
+
import type { Prettify } from '../lib/type_util';
|
|
14
|
+
import {
|
|
15
|
+
type Query,
|
|
16
|
+
toSql,
|
|
17
|
+
type BooleanExpr,
|
|
18
|
+
evaluateBooleanExpr,
|
|
19
|
+
getQueryAccessorName,
|
|
20
|
+
getQueryWhereClause,
|
|
21
|
+
} from '../lib/query';
|
|
22
|
+
|
|
23
|
+
export interface UseTableCallbacks<RowType> {
|
|
24
|
+
onInsert?: (row: RowType) => void;
|
|
25
|
+
onDelete?: (row: RowType) => void;
|
|
26
|
+
onUpdate?: (oldRow: RowType, newRow: RowType) => void;
|
|
27
|
+
/** Whether the subscription is active. Defaults to `true`. */
|
|
28
|
+
enabled?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type MembershipChange = 'enter' | 'leave' | 'stayIn' | 'stayOut';
|
|
32
|
+
|
|
33
|
+
function classifyMembership(
|
|
34
|
+
whereExpr: BooleanExpr<any> | undefined,
|
|
35
|
+
oldRow: Record<string, any>,
|
|
36
|
+
newRow: Record<string, any>
|
|
37
|
+
): MembershipChange {
|
|
38
|
+
if (!whereExpr) return 'stayIn';
|
|
39
|
+
const oldIn = evaluateBooleanExpr(whereExpr, oldRow);
|
|
40
|
+
const newIn = evaluateBooleanExpr(whereExpr, newRow);
|
|
41
|
+
if (oldIn && !newIn) return 'leave';
|
|
42
|
+
if (!oldIn && newIn) return 'enter';
|
|
43
|
+
if (oldIn && newIn) return 'stayIn';
|
|
44
|
+
return 'stayOut';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* React hook to subscribe to a table in SpacetimeDB and receive live updates.
|
|
49
|
+
*
|
|
50
|
+
* Accepts a query builder expression as the first argument:
|
|
51
|
+
* - `tables.user` — subscribe to all rows
|
|
52
|
+
* - `tables.user.where(r => r.online.eq(true))` — subscribe with a filter
|
|
53
|
+
*
|
|
54
|
+
* @param query - A query builder expression (table reference or filtered query).
|
|
55
|
+
* @param callbacks - Optional callbacks for row insert, delete, and update events.
|
|
56
|
+
* @returns A tuple of [rows, isReady].
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* const [rows, isReady] = useTable(tables.user);
|
|
61
|
+
* const [onlineUsers, isReady] = useTable(
|
|
62
|
+
* tables.user.where(r => r.online.eq(true)),
|
|
63
|
+
* { onInsert: (row) => console.log('New user:', row) }
|
|
64
|
+
* );
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function useTable<TableDef extends UntypedTableDef>(
|
|
68
|
+
query: Query<TableDef>,
|
|
69
|
+
callbacks?: UseTableCallbacks<Prettify<RowType<TableDef>>>
|
|
70
|
+
): [readonly Prettify<RowType<TableDef>>[], boolean] {
|
|
71
|
+
type UseTableRowType = RowType<TableDef>;
|
|
72
|
+
const enabled = callbacks?.enabled ?? true;
|
|
73
|
+
const accessorName = getQueryAccessorName(query);
|
|
74
|
+
const whereExpr = getQueryWhereClause(query);
|
|
75
|
+
|
|
76
|
+
const [subscribeApplied, setSubscribeApplied] = useState(false);
|
|
77
|
+
let connectionState: ConnectionState | undefined;
|
|
78
|
+
try {
|
|
79
|
+
connectionState = useSpacetimeDB();
|
|
80
|
+
} catch {
|
|
81
|
+
throw new Error(
|
|
82
|
+
'Could not find SpacetimeDB client! Did you forget to add a ' +
|
|
83
|
+
'`SpacetimeDBProvider`? `useTable` must be used in the React component tree ' +
|
|
84
|
+
'under a `SpacetimeDBProvider` component.'
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const querySql = toSql(query);
|
|
89
|
+
|
|
90
|
+
const latestTransactionEventId = useRef<string | null>(null);
|
|
91
|
+
const lastSnapshotRef = useRef<
|
|
92
|
+
[readonly Prettify<UseTableRowType>[], boolean] | null
|
|
93
|
+
>(null);
|
|
94
|
+
|
|
95
|
+
const computeSnapshot = useCallback((): [
|
|
96
|
+
readonly Prettify<UseTableRowType>[],
|
|
97
|
+
boolean,
|
|
98
|
+
] => {
|
|
99
|
+
if (!enabled) {
|
|
100
|
+
return [[], true];
|
|
101
|
+
}
|
|
102
|
+
const connection = connectionState.getConnection();
|
|
103
|
+
if (!connection) {
|
|
104
|
+
return [[], false];
|
|
105
|
+
}
|
|
106
|
+
const table = connection.db[accessorName];
|
|
107
|
+
const result: readonly Prettify<UseTableRowType>[] = whereExpr
|
|
108
|
+
? (Array.from(table.iter()).filter(row =>
|
|
109
|
+
evaluateBooleanExpr(whereExpr, row as Record<string, any>)
|
|
110
|
+
) as Prettify<UseTableRowType>[])
|
|
111
|
+
: (Array.from(table.iter()) as Prettify<UseTableRowType>[]);
|
|
112
|
+
return [result, subscribeApplied];
|
|
113
|
+
// TODO: investigating refactoring so that this is no longer necessary, as we have had genuine bugs with missed deps.
|
|
114
|
+
// See https://github.com/clockworklabs/SpacetimeDB/pull/4580.
|
|
115
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
116
|
+
}, [connectionState, accessorName, querySql, subscribeApplied, enabled]);
|
|
117
|
+
|
|
118
|
+
// Invalidate the cached snapshot when computeSnapshot changes (e.g. when
|
|
119
|
+
// subscribeApplied flips to true) so getSnapshot() recomputes on the next
|
|
120
|
+
// render instead of returning a stale [rows, false] tuple.
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
lastSnapshotRef.current = null;
|
|
123
|
+
}, [computeSnapshot]);
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (!enabled) {
|
|
127
|
+
setSubscribeApplied(false);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const connection = connectionState.getConnection();
|
|
131
|
+
if (!connectionState.isActive || !connection) {
|
|
132
|
+
// The connection dropped (or was replaced and has not reconnected
|
|
133
|
+
// yet), so any previously applied subscription no longer reflects the
|
|
134
|
+
// current cache. Report not-ready until the new subscription applies.
|
|
135
|
+
setSubscribeApplied(false);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const cancel = connection
|
|
139
|
+
.subscriptionBuilder()
|
|
140
|
+
.onApplied(() => {
|
|
141
|
+
setSubscribeApplied(true);
|
|
142
|
+
})
|
|
143
|
+
.subscribe(querySql);
|
|
144
|
+
return () => {
|
|
145
|
+
cancel.unsubscribe();
|
|
146
|
+
};
|
|
147
|
+
}, [querySql, connectionState.isActive, connectionState, enabled]);
|
|
148
|
+
|
|
149
|
+
const subscribe = useCallback(
|
|
150
|
+
(onStoreChange: () => void) => {
|
|
151
|
+
if (!enabled) {
|
|
152
|
+
return () => {};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const onInsert = (
|
|
156
|
+
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
157
|
+
row: any
|
|
158
|
+
) => {
|
|
159
|
+
if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
callbacks?.onInsert?.(row);
|
|
163
|
+
if (ctx.event.id !== latestTransactionEventId.current) {
|
|
164
|
+
latestTransactionEventId.current = ctx.event.id;
|
|
165
|
+
lastSnapshotRef.current = computeSnapshot();
|
|
166
|
+
onStoreChange();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const onDelete = (
|
|
171
|
+
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
172
|
+
row: any
|
|
173
|
+
) => {
|
|
174
|
+
if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
callbacks?.onDelete?.(row);
|
|
178
|
+
if (ctx.event.id !== latestTransactionEventId.current) {
|
|
179
|
+
latestTransactionEventId.current = ctx.event.id;
|
|
180
|
+
lastSnapshotRef.current = computeSnapshot();
|
|
181
|
+
onStoreChange();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const onUpdate = (
|
|
186
|
+
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
187
|
+
oldRow: any,
|
|
188
|
+
newRow: any
|
|
189
|
+
) => {
|
|
190
|
+
const change = classifyMembership(whereExpr, oldRow, newRow);
|
|
191
|
+
|
|
192
|
+
switch (change) {
|
|
193
|
+
case 'leave':
|
|
194
|
+
callbacks?.onDelete?.(oldRow);
|
|
195
|
+
break;
|
|
196
|
+
case 'enter':
|
|
197
|
+
callbacks?.onInsert?.(newRow);
|
|
198
|
+
break;
|
|
199
|
+
case 'stayIn':
|
|
200
|
+
callbacks?.onUpdate?.(oldRow, newRow);
|
|
201
|
+
break;
|
|
202
|
+
case 'stayOut':
|
|
203
|
+
return; // no-op
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (ctx.event.id !== latestTransactionEventId.current) {
|
|
207
|
+
latestTransactionEventId.current = ctx.event.id;
|
|
208
|
+
lastSnapshotRef.current = computeSnapshot();
|
|
209
|
+
onStoreChange();
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const connection = connectionState.getConnection();
|
|
214
|
+
if (!connection) {
|
|
215
|
+
return () => {};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const table = connection.db[accessorName];
|
|
219
|
+
table.onInsert(onInsert);
|
|
220
|
+
table.onDelete(onDelete);
|
|
221
|
+
table.onUpdate?.(onUpdate);
|
|
222
|
+
|
|
223
|
+
return () => {
|
|
224
|
+
table.removeOnInsert(onInsert);
|
|
225
|
+
table.removeOnDelete(onDelete);
|
|
226
|
+
table.removeOnUpdate?.(onUpdate);
|
|
227
|
+
};
|
|
228
|
+
},
|
|
229
|
+
// TODO: investigating refactoring so that this is no longer necessary, as we have had genuine bugs with missed deps.
|
|
230
|
+
// See https://github.com/clockworklabs/SpacetimeDB/pull/4580.
|
|
231
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
232
|
+
[
|
|
233
|
+
connectionState,
|
|
234
|
+
accessorName,
|
|
235
|
+
querySql,
|
|
236
|
+
computeSnapshot,
|
|
237
|
+
callbacks?.onDelete,
|
|
238
|
+
callbacks?.onInsert,
|
|
239
|
+
callbacks?.onUpdate,
|
|
240
|
+
enabled,
|
|
241
|
+
]
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
const getSnapshot = useCallback((): [
|
|
245
|
+
readonly Prettify<UseTableRowType>[],
|
|
246
|
+
boolean,
|
|
247
|
+
] => {
|
|
248
|
+
if (!lastSnapshotRef.current) {
|
|
249
|
+
lastSnapshotRef.current = computeSnapshot();
|
|
250
|
+
}
|
|
251
|
+
return lastSnapshotRef.current;
|
|
252
|
+
}, [computeSnapshot]);
|
|
253
|
+
|
|
254
|
+
// SSR fallback can be the same getter
|
|
255
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
256
|
+
}
|