spacetimedb 2.5.0 → 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 -211
- 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 +120 -50
- package/dist/browser/solid/index.mjs.map +1 -1
- 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/index.cjs +120 -50
- package/dist/solid/index.cjs.map +1 -1
- package/dist/solid/index.mjs +120 -50
- package/dist/solid/index.mjs.map +1 -1
- 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 +1 -1
- 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 -97
- package/src/solid/connection_state.ts +6 -6
- package/src/solid/index.ts +5 -5
- package/src/solid/useProcedure.ts +57 -57
- package/src/solid/useReducer.ts +50 -50
- package/src/solid/useSpacetimeDB.ts +18 -18
- package/src/solid/useTable.ts +203 -203
- 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
|
@@ -1,234 +1,234 @@
|
|
|
1
|
-
import {
|
|
2
|
-
assertInInjectionContext,
|
|
3
|
-
inject,
|
|
4
|
-
signal,
|
|
5
|
-
effect,
|
|
6
|
-
type Signal,
|
|
7
|
-
} from '@angular/core';
|
|
8
|
-
import type { RowType, UntypedTableDef } from '../../lib/table';
|
|
9
|
-
import type { Prettify } from '../../lib/type_util';
|
|
10
|
-
import { SPACETIMEDB_CONNECTION } from '../connection_state';
|
|
11
|
-
import {
|
|
12
|
-
type Query,
|
|
13
|
-
type BooleanExpr,
|
|
14
|
-
toSql,
|
|
15
|
-
evaluateBooleanExpr,
|
|
16
|
-
getQueryAccessorName,
|
|
17
|
-
getQueryWhereClause,
|
|
18
|
-
} from '../../lib/query';
|
|
19
|
-
import type { EventContextInterface } from '../../sdk';
|
|
20
|
-
import type { UntypedRemoteModule } from '../../sdk/spacetime_module';
|
|
21
|
-
|
|
22
|
-
export type RowTypeDef<TableDef extends UntypedTableDef> = Prettify<
|
|
23
|
-
RowType<TableDef>
|
|
24
|
-
>;
|
|
25
|
-
|
|
26
|
-
export interface TableRows<TableDef extends UntypedTableDef> {
|
|
27
|
-
rows: readonly RowTypeDef<TableDef>[];
|
|
28
|
-
isLoading: boolean;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface InjectTableCallbacks<RowType> {
|
|
32
|
-
onInsert?: (row: RowType) => void;
|
|
33
|
-
onDelete?: (row: RowType) => void;
|
|
34
|
-
onUpdate?: (oldRow: RowType, newRow: RowType) => void;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type MembershipChange = 'enter' | 'leave' | 'stayIn' | 'stayOut';
|
|
38
|
-
|
|
39
|
-
function classifyMembership(
|
|
40
|
-
whereExpr: BooleanExpr<any> | undefined,
|
|
41
|
-
oldRow: Record<string, any>,
|
|
42
|
-
newRow: Record<string, any>
|
|
43
|
-
): MembershipChange {
|
|
44
|
-
if (!whereExpr) return 'stayIn';
|
|
45
|
-
const oldIn = evaluateBooleanExpr(whereExpr, oldRow);
|
|
46
|
-
const newIn = evaluateBooleanExpr(whereExpr, newRow);
|
|
47
|
-
if (oldIn && !newIn) return 'leave';
|
|
48
|
-
if (!oldIn && newIn) return 'enter';
|
|
49
|
-
if (oldIn && newIn) return 'stayIn';
|
|
50
|
-
return 'stayOut';
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Angular injection function to subscribe to a table in SpacetimeDB and receive live updates.
|
|
55
|
-
*
|
|
56
|
-
* Must be called within an injection context (component field initializer or constructor).
|
|
57
|
-
*
|
|
58
|
-
* Accepts a query builder expression as the first argument:
|
|
59
|
-
* - `tables.user` — subscribe to all rows
|
|
60
|
-
* - `tables.user.where(r => r.online.eq(true))` — subscribe with a filter
|
|
61
|
-
*
|
|
62
|
-
* @template TableDef The table definition type.
|
|
63
|
-
*
|
|
64
|
-
* @param query - A query builder expression (table reference or filtered query).
|
|
65
|
-
* @param callbacks - Optional callbacks for row insert, delete, and update events.
|
|
66
|
-
*
|
|
67
|
-
* @returns A signal containing the current rows and loading state.
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```typescript
|
|
71
|
-
* export class UsersComponent {
|
|
72
|
-
* users = injectTable(tables.user);
|
|
73
|
-
*
|
|
74
|
-
* // With a filter:
|
|
75
|
-
* onlineUsers = injectTable(
|
|
76
|
-
* tables.user.where(r => r.online.eq(true)),
|
|
77
|
-
* {
|
|
78
|
-
* onInsert: (row) => console.log('Inserted:', row),
|
|
79
|
-
* onDelete: (row) => console.log('Deleted:', row),
|
|
80
|
-
* onUpdate: (oldRow, newRow) => console.log('Updated:', oldRow, newRow),
|
|
81
|
-
* }
|
|
82
|
-
* );
|
|
83
|
-
*
|
|
84
|
-
* // In template: {{ users().rows.length }} users
|
|
85
|
-
* // Loading state: {{ users().isLoading }}
|
|
86
|
-
* }
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
export function injectTable<TableDef extends UntypedTableDef>(
|
|
90
|
-
query: Query<TableDef>,
|
|
91
|
-
callbacks?: InjectTableCallbacks<RowTypeDef<TableDef>>
|
|
92
|
-
): Signal<TableRows<TableDef>> {
|
|
93
|
-
assertInInjectionContext(injectTable);
|
|
94
|
-
|
|
95
|
-
const connState = inject(SPACETIMEDB_CONNECTION);
|
|
96
|
-
|
|
97
|
-
const accessorName = getQueryAccessorName(query);
|
|
98
|
-
const whereExpr = getQueryWhereClause(query);
|
|
99
|
-
const querySql = toSql(query);
|
|
100
|
-
|
|
101
|
-
const tableSignal = signal<TableRows<TableDef>>({
|
|
102
|
-
isLoading: true,
|
|
103
|
-
rows: [],
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
let latestTransactionEvent: any = null;
|
|
107
|
-
let subscribeApplied = false;
|
|
108
|
-
|
|
109
|
-
// Note: this code is mostly derived from the React useTable implementation
|
|
110
|
-
// in order to keep behavior consistent across frameworks.
|
|
111
|
-
|
|
112
|
-
const computeSnapshot = (): readonly RowTypeDef<TableDef>[] => {
|
|
113
|
-
const state = connState();
|
|
114
|
-
if (!state.isActive) {
|
|
115
|
-
return [];
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const connection = state.getConnection();
|
|
119
|
-
if (!connection) {
|
|
120
|
-
return [];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const table = connection.db[accessorName];
|
|
124
|
-
|
|
125
|
-
if (whereExpr) {
|
|
126
|
-
return Array.from(table.iter()).filter(row =>
|
|
127
|
-
evaluateBooleanExpr(whereExpr, row as Record<string, any>)
|
|
128
|
-
) as RowTypeDef<TableDef>[];
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return Array.from(table.iter()) as RowTypeDef<TableDef>[];
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const updateSnapshot = () => {
|
|
135
|
-
tableSignal.set({
|
|
136
|
-
rows: computeSnapshot(),
|
|
137
|
-
isLoading: !subscribeApplied,
|
|
138
|
-
});
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
effect((onCleanup: (fn: () => void) => void) => {
|
|
142
|
-
const state = connState();
|
|
143
|
-
if (!state.isActive) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const connection = state.getConnection();
|
|
148
|
-
if (!connection) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const table = connection.db[accessorName];
|
|
153
|
-
|
|
154
|
-
const onInsert = (
|
|
155
|
-
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
156
|
-
row: any
|
|
157
|
-
) => {
|
|
158
|
-
if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
callbacks?.onInsert?.(row);
|
|
163
|
-
|
|
164
|
-
if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) {
|
|
165
|
-
latestTransactionEvent = ctx.event;
|
|
166
|
-
updateSnapshot();
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const onDelete = (
|
|
171
|
-
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
172
|
-
row: any
|
|
173
|
-
) => {
|
|
174
|
-
if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) {
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
callbacks?.onDelete?.(row);
|
|
179
|
-
|
|
180
|
-
if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) {
|
|
181
|
-
latestTransactionEvent = ctx.event;
|
|
182
|
-
updateSnapshot();
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
const onUpdate = (
|
|
187
|
-
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
188
|
-
oldRow: any,
|
|
189
|
-
newRow: any
|
|
190
|
-
) => {
|
|
191
|
-
const change = classifyMembership(whereExpr, oldRow, newRow);
|
|
192
|
-
|
|
193
|
-
switch (change) {
|
|
194
|
-
case 'leave':
|
|
195
|
-
callbacks?.onDelete?.(oldRow);
|
|
196
|
-
break;
|
|
197
|
-
case 'enter':
|
|
198
|
-
callbacks?.onInsert?.(newRow);
|
|
199
|
-
break;
|
|
200
|
-
case 'stayIn':
|
|
201
|
-
callbacks?.onUpdate?.(oldRow, newRow);
|
|
202
|
-
break;
|
|
203
|
-
case 'stayOut':
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) {
|
|
208
|
-
latestTransactionEvent = ctx.event;
|
|
209
|
-
updateSnapshot();
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
table.onInsert(onInsert);
|
|
214
|
-
table.onDelete(onDelete);
|
|
215
|
-
table.onUpdate?.(onUpdate);
|
|
216
|
-
|
|
217
|
-
const subscription = connection
|
|
218
|
-
.subscriptionBuilder()
|
|
219
|
-
.onApplied(() => {
|
|
220
|
-
subscribeApplied = true;
|
|
221
|
-
updateSnapshot();
|
|
222
|
-
})
|
|
223
|
-
.subscribe(querySql);
|
|
224
|
-
|
|
225
|
-
onCleanup(() => {
|
|
226
|
-
table.removeOnInsert(onInsert);
|
|
227
|
-
table.removeOnDelete(onDelete);
|
|
228
|
-
table.removeOnUpdate?.(onUpdate);
|
|
229
|
-
subscription.unsubscribe();
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
return tableSignal.asReadonly();
|
|
234
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
assertInInjectionContext,
|
|
3
|
+
inject,
|
|
4
|
+
signal,
|
|
5
|
+
effect,
|
|
6
|
+
type Signal,
|
|
7
|
+
} from '@angular/core';
|
|
8
|
+
import type { RowType, UntypedTableDef } from '../../lib/table';
|
|
9
|
+
import type { Prettify } from '../../lib/type_util';
|
|
10
|
+
import { SPACETIMEDB_CONNECTION } from '../connection_state';
|
|
11
|
+
import {
|
|
12
|
+
type Query,
|
|
13
|
+
type BooleanExpr,
|
|
14
|
+
toSql,
|
|
15
|
+
evaluateBooleanExpr,
|
|
16
|
+
getQueryAccessorName,
|
|
17
|
+
getQueryWhereClause,
|
|
18
|
+
} from '../../lib/query';
|
|
19
|
+
import type { EventContextInterface } from '../../sdk';
|
|
20
|
+
import type { UntypedRemoteModule } from '../../sdk/spacetime_module';
|
|
21
|
+
|
|
22
|
+
export type RowTypeDef<TableDef extends UntypedTableDef> = Prettify<
|
|
23
|
+
RowType<TableDef>
|
|
24
|
+
>;
|
|
25
|
+
|
|
26
|
+
export interface TableRows<TableDef extends UntypedTableDef> {
|
|
27
|
+
rows: readonly RowTypeDef<TableDef>[];
|
|
28
|
+
isLoading: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface InjectTableCallbacks<RowType> {
|
|
32
|
+
onInsert?: (row: RowType) => void;
|
|
33
|
+
onDelete?: (row: RowType) => void;
|
|
34
|
+
onUpdate?: (oldRow: RowType, newRow: RowType) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type MembershipChange = 'enter' | 'leave' | 'stayIn' | 'stayOut';
|
|
38
|
+
|
|
39
|
+
function classifyMembership(
|
|
40
|
+
whereExpr: BooleanExpr<any> | undefined,
|
|
41
|
+
oldRow: Record<string, any>,
|
|
42
|
+
newRow: Record<string, any>
|
|
43
|
+
): MembershipChange {
|
|
44
|
+
if (!whereExpr) return 'stayIn';
|
|
45
|
+
const oldIn = evaluateBooleanExpr(whereExpr, oldRow);
|
|
46
|
+
const newIn = evaluateBooleanExpr(whereExpr, newRow);
|
|
47
|
+
if (oldIn && !newIn) return 'leave';
|
|
48
|
+
if (!oldIn && newIn) return 'enter';
|
|
49
|
+
if (oldIn && newIn) return 'stayIn';
|
|
50
|
+
return 'stayOut';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Angular injection function to subscribe to a table in SpacetimeDB and receive live updates.
|
|
55
|
+
*
|
|
56
|
+
* Must be called within an injection context (component field initializer or constructor).
|
|
57
|
+
*
|
|
58
|
+
* Accepts a query builder expression as the first argument:
|
|
59
|
+
* - `tables.user` — subscribe to all rows
|
|
60
|
+
* - `tables.user.where(r => r.online.eq(true))` — subscribe with a filter
|
|
61
|
+
*
|
|
62
|
+
* @template TableDef The table definition type.
|
|
63
|
+
*
|
|
64
|
+
* @param query - A query builder expression (table reference or filtered query).
|
|
65
|
+
* @param callbacks - Optional callbacks for row insert, delete, and update events.
|
|
66
|
+
*
|
|
67
|
+
* @returns A signal containing the current rows and loading state.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* export class UsersComponent {
|
|
72
|
+
* users = injectTable(tables.user);
|
|
73
|
+
*
|
|
74
|
+
* // With a filter:
|
|
75
|
+
* onlineUsers = injectTable(
|
|
76
|
+
* tables.user.where(r => r.online.eq(true)),
|
|
77
|
+
* {
|
|
78
|
+
* onInsert: (row) => console.log('Inserted:', row),
|
|
79
|
+
* onDelete: (row) => console.log('Deleted:', row),
|
|
80
|
+
* onUpdate: (oldRow, newRow) => console.log('Updated:', oldRow, newRow),
|
|
81
|
+
* }
|
|
82
|
+
* );
|
|
83
|
+
*
|
|
84
|
+
* // In template: {{ users().rows.length }} users
|
|
85
|
+
* // Loading state: {{ users().isLoading }}
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export function injectTable<TableDef extends UntypedTableDef>(
|
|
90
|
+
query: Query<TableDef>,
|
|
91
|
+
callbacks?: InjectTableCallbacks<RowTypeDef<TableDef>>
|
|
92
|
+
): Signal<TableRows<TableDef>> {
|
|
93
|
+
assertInInjectionContext(injectTable);
|
|
94
|
+
|
|
95
|
+
const connState = inject(SPACETIMEDB_CONNECTION);
|
|
96
|
+
|
|
97
|
+
const accessorName = getQueryAccessorName(query);
|
|
98
|
+
const whereExpr = getQueryWhereClause(query);
|
|
99
|
+
const querySql = toSql(query);
|
|
100
|
+
|
|
101
|
+
const tableSignal = signal<TableRows<TableDef>>({
|
|
102
|
+
isLoading: true,
|
|
103
|
+
rows: [],
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
let latestTransactionEvent: any = null;
|
|
107
|
+
let subscribeApplied = false;
|
|
108
|
+
|
|
109
|
+
// Note: this code is mostly derived from the React useTable implementation
|
|
110
|
+
// in order to keep behavior consistent across frameworks.
|
|
111
|
+
|
|
112
|
+
const computeSnapshot = (): readonly RowTypeDef<TableDef>[] => {
|
|
113
|
+
const state = connState();
|
|
114
|
+
if (!state.isActive) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const connection = state.getConnection();
|
|
119
|
+
if (!connection) {
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const table = connection.db[accessorName];
|
|
124
|
+
|
|
125
|
+
if (whereExpr) {
|
|
126
|
+
return Array.from(table.iter()).filter(row =>
|
|
127
|
+
evaluateBooleanExpr(whereExpr, row as Record<string, any>)
|
|
128
|
+
) as RowTypeDef<TableDef>[];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return Array.from(table.iter()) as RowTypeDef<TableDef>[];
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const updateSnapshot = () => {
|
|
135
|
+
tableSignal.set({
|
|
136
|
+
rows: computeSnapshot(),
|
|
137
|
+
isLoading: !subscribeApplied,
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
effect((onCleanup: (fn: () => void) => void) => {
|
|
142
|
+
const state = connState();
|
|
143
|
+
if (!state.isActive) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const connection = state.getConnection();
|
|
148
|
+
if (!connection) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const table = connection.db[accessorName];
|
|
153
|
+
|
|
154
|
+
const onInsert = (
|
|
155
|
+
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
156
|
+
row: any
|
|
157
|
+
) => {
|
|
158
|
+
if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
callbacks?.onInsert?.(row);
|
|
163
|
+
|
|
164
|
+
if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) {
|
|
165
|
+
latestTransactionEvent = ctx.event;
|
|
166
|
+
updateSnapshot();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const onDelete = (
|
|
171
|
+
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
172
|
+
row: any
|
|
173
|
+
) => {
|
|
174
|
+
if (whereExpr && !evaluateBooleanExpr(whereExpr, row)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
callbacks?.onDelete?.(row);
|
|
179
|
+
|
|
180
|
+
if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) {
|
|
181
|
+
latestTransactionEvent = ctx.event;
|
|
182
|
+
updateSnapshot();
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const onUpdate = (
|
|
187
|
+
ctx: EventContextInterface<UntypedRemoteModule>,
|
|
188
|
+
oldRow: any,
|
|
189
|
+
newRow: any
|
|
190
|
+
) => {
|
|
191
|
+
const change = classifyMembership(whereExpr, oldRow, newRow);
|
|
192
|
+
|
|
193
|
+
switch (change) {
|
|
194
|
+
case 'leave':
|
|
195
|
+
callbacks?.onDelete?.(oldRow);
|
|
196
|
+
break;
|
|
197
|
+
case 'enter':
|
|
198
|
+
callbacks?.onInsert?.(newRow);
|
|
199
|
+
break;
|
|
200
|
+
case 'stayIn':
|
|
201
|
+
callbacks?.onUpdate?.(oldRow, newRow);
|
|
202
|
+
break;
|
|
203
|
+
case 'stayOut':
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (ctx.event !== latestTransactionEvent || !latestTransactionEvent) {
|
|
208
|
+
latestTransactionEvent = ctx.event;
|
|
209
|
+
updateSnapshot();
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
table.onInsert(onInsert);
|
|
214
|
+
table.onDelete(onDelete);
|
|
215
|
+
table.onUpdate?.(onUpdate);
|
|
216
|
+
|
|
217
|
+
const subscription = connection
|
|
218
|
+
.subscriptionBuilder()
|
|
219
|
+
.onApplied(() => {
|
|
220
|
+
subscribeApplied = true;
|
|
221
|
+
updateSnapshot();
|
|
222
|
+
})
|
|
223
|
+
.subscribe(querySql);
|
|
224
|
+
|
|
225
|
+
onCleanup(() => {
|
|
226
|
+
table.removeOnInsert(onInsert);
|
|
227
|
+
table.removeOnDelete(onDelete);
|
|
228
|
+
table.removeOnUpdate?.(onUpdate);
|
|
229
|
+
subscription.unsubscribe();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
return tableSignal.asReadonly();
|
|
234
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { provideSpacetimeDB } from './provide-spacetimedb.ts';
|
|
1
|
+
export { provideSpacetimeDB } from './provide-spacetimedb.ts';
|
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
import {
|
|
2
|
-
makeEnvironmentProviders,
|
|
3
|
-
provideAppInitializer,
|
|
4
|
-
signal,
|
|
5
|
-
type EnvironmentProviders,
|
|
6
|
-
} from '@angular/core';
|
|
7
|
-
import type {
|
|
8
|
-
DbConnectionBuilder,
|
|
9
|
-
DbConnectionImpl,
|
|
10
|
-
ErrorContextInterface,
|
|
11
|
-
RemoteModuleOf,
|
|
12
|
-
} from '../../sdk/db_connection_impl';
|
|
13
|
-
import {
|
|
14
|
-
SPACETIMEDB_CONNECTION,
|
|
15
|
-
type ConnectionState,
|
|
16
|
-
} from '../connection_state';
|
|
17
|
-
import { ConnectionId } from '../../lib/connection_id';
|
|
18
|
-
|
|
19
|
-
let connRef: DbConnectionImpl<any> | null = null;
|
|
20
|
-
|
|
21
|
-
export function provideSpacetimeDB<DbConnection extends DbConnectionImpl<any>>(
|
|
22
|
-
connectionBuilder: DbConnectionBuilder<DbConnection>
|
|
23
|
-
): EnvironmentProviders {
|
|
24
|
-
const state = signal<ConnectionState>({
|
|
25
|
-
isActive: false,
|
|
26
|
-
identity: undefined,
|
|
27
|
-
token: undefined,
|
|
28
|
-
connectionId: ConnectionId.random(),
|
|
29
|
-
connectionError: undefined,
|
|
30
|
-
getConnection: () => null,
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
return makeEnvironmentProviders([
|
|
34
|
-
{ provide: SPACETIMEDB_CONNECTION, useValue: state },
|
|
35
|
-
provideAppInitializer(() => {
|
|
36
|
-
if (typeof window === 'undefined') {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const getConnection = <T extends DbConnectionImpl<any>>() =>
|
|
41
|
-
connRef as T | null;
|
|
42
|
-
|
|
43
|
-
if (!connRef) {
|
|
44
|
-
connRef = connectionBuilder.build();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const onConnect = (conn: DbConnection) => {
|
|
48
|
-
state.set({
|
|
49
|
-
...state(),
|
|
50
|
-
isActive: conn.isActive,
|
|
51
|
-
identity: conn.identity,
|
|
52
|
-
token: conn.token,
|
|
53
|
-
connectionId: conn.connectionId,
|
|
54
|
-
getConnection,
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const onDisconnect = (
|
|
59
|
-
ctx: ErrorContextInterface<RemoteModuleOf<DbConnection>>
|
|
60
|
-
) => {
|
|
61
|
-
state.set({
|
|
62
|
-
...state(),
|
|
63
|
-
isActive: ctx.isActive,
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const onConnectError = (
|
|
68
|
-
ctx: ErrorContextInterface<RemoteModuleOf<DbConnection>>,
|
|
69
|
-
err: Error
|
|
70
|
-
) => {
|
|
71
|
-
state.set({
|
|
72
|
-
...state(),
|
|
73
|
-
isActive: ctx.isActive,
|
|
74
|
-
connectionError: err,
|
|
75
|
-
});
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
connectionBuilder.onConnect(onConnect);
|
|
79
|
-
connectionBuilder.onDisconnect(onDisconnect);
|
|
80
|
-
connectionBuilder.onConnectError(onConnectError);
|
|
81
|
-
|
|
82
|
-
// sync initial state if already connected
|
|
83
|
-
const conn = connRef;
|
|
84
|
-
if (conn) {
|
|
85
|
-
state.set({
|
|
86
|
-
...state(),
|
|
87
|
-
isActive: conn.isActive,
|
|
88
|
-
identity: conn.identity,
|
|
89
|
-
token: conn.token,
|
|
90
|
-
connectionId: conn.connectionId,
|
|
91
|
-
getConnection,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}),
|
|
95
|
-
]);
|
|
96
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
makeEnvironmentProviders,
|
|
3
|
+
provideAppInitializer,
|
|
4
|
+
signal,
|
|
5
|
+
type EnvironmentProviders,
|
|
6
|
+
} from '@angular/core';
|
|
7
|
+
import type {
|
|
8
|
+
DbConnectionBuilder,
|
|
9
|
+
DbConnectionImpl,
|
|
10
|
+
ErrorContextInterface,
|
|
11
|
+
RemoteModuleOf,
|
|
12
|
+
} from '../../sdk/db_connection_impl';
|
|
13
|
+
import {
|
|
14
|
+
SPACETIMEDB_CONNECTION,
|
|
15
|
+
type ConnectionState,
|
|
16
|
+
} from '../connection_state';
|
|
17
|
+
import { ConnectionId } from '../../lib/connection_id';
|
|
18
|
+
|
|
19
|
+
let connRef: DbConnectionImpl<any> | null = null;
|
|
20
|
+
|
|
21
|
+
export function provideSpacetimeDB<DbConnection extends DbConnectionImpl<any>>(
|
|
22
|
+
connectionBuilder: DbConnectionBuilder<DbConnection>
|
|
23
|
+
): EnvironmentProviders {
|
|
24
|
+
const state = signal<ConnectionState>({
|
|
25
|
+
isActive: false,
|
|
26
|
+
identity: undefined,
|
|
27
|
+
token: undefined,
|
|
28
|
+
connectionId: ConnectionId.random(),
|
|
29
|
+
connectionError: undefined,
|
|
30
|
+
getConnection: () => null,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return makeEnvironmentProviders([
|
|
34
|
+
{ provide: SPACETIMEDB_CONNECTION, useValue: state },
|
|
35
|
+
provideAppInitializer(() => {
|
|
36
|
+
if (typeof window === 'undefined') {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const getConnection = <T extends DbConnectionImpl<any>>() =>
|
|
41
|
+
connRef as T | null;
|
|
42
|
+
|
|
43
|
+
if (!connRef) {
|
|
44
|
+
connRef = connectionBuilder.build();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const onConnect = (conn: DbConnection) => {
|
|
48
|
+
state.set({
|
|
49
|
+
...state(),
|
|
50
|
+
isActive: conn.isActive,
|
|
51
|
+
identity: conn.identity,
|
|
52
|
+
token: conn.token,
|
|
53
|
+
connectionId: conn.connectionId,
|
|
54
|
+
getConnection,
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const onDisconnect = (
|
|
59
|
+
ctx: ErrorContextInterface<RemoteModuleOf<DbConnection>>
|
|
60
|
+
) => {
|
|
61
|
+
state.set({
|
|
62
|
+
...state(),
|
|
63
|
+
isActive: ctx.isActive,
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const onConnectError = (
|
|
68
|
+
ctx: ErrorContextInterface<RemoteModuleOf<DbConnection>>,
|
|
69
|
+
err: Error
|
|
70
|
+
) => {
|
|
71
|
+
state.set({
|
|
72
|
+
...state(),
|
|
73
|
+
isActive: ctx.isActive,
|
|
74
|
+
connectionError: err,
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
connectionBuilder.onConnect(onConnect);
|
|
79
|
+
connectionBuilder.onDisconnect(onDisconnect);
|
|
80
|
+
connectionBuilder.onConnectError(onConnectError);
|
|
81
|
+
|
|
82
|
+
// sync initial state if already connected
|
|
83
|
+
const conn = connRef;
|
|
84
|
+
if (conn) {
|
|
85
|
+
state.set({
|
|
86
|
+
...state(),
|
|
87
|
+
isActive: conn.isActive,
|
|
88
|
+
identity: conn.identity,
|
|
89
|
+
token: conn.token,
|
|
90
|
+
connectionId: conn.connectionId,
|
|
91
|
+
getConnection,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}),
|
|
95
|
+
]);
|
|
96
|
+
}
|