Skip to content

Commit dd34adf

Browse files
Implemented the new mutable subscriptions API (#135)
* Implemented the new subscriptions API * Fixed the ../index thing * Fixed the ../index thing for realz * Fixes for Timestamp and TimeDuration * Better error handling * Fixed rebase issues * lint * Reduce diff * lint
1 parent 59157e1 commit dd34adf

24 files changed

+880
-541
lines changed

examples/quickstart-chat/src/App.tsx

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import React, { useEffect, useState } from 'react';
22
import './App.css';
3-
import { DBConnection, EventContext, Message, User } from './module_bindings';
3+
import {
4+
DBConnection,
5+
ErrorContext,
6+
EventContext,
7+
Message,
8+
User,
9+
} from './module_bindings';
410
import { Identity } from '@clockworklabs/spacetimedb-sdk';
511

612
export type PrettyMessage = {
@@ -85,6 +91,21 @@ function App() {
8591
const [conn, setConn] = useState<DBConnection | null>(null);
8692

8793
useEffect(() => {
94+
const subscribeToQueries = (conn: DBConnection, queries: string[]) => {
95+
let count = 0;
96+
for (const query of queries) {
97+
conn
98+
?.subscriptionBuilder()
99+
.onApplied(() => {
100+
count++;
101+
if (count === queries.length) {
102+
console.log('SDK client cache initialized.');
103+
}
104+
})
105+
.subscribe(query);
106+
}
107+
};
108+
88109
const onConnect = (
89110
conn: DBConnection,
90111
identity: Identity,
@@ -100,20 +121,16 @@ function App() {
100121
conn.reducers.onSendMessage(() => {
101122
console.log('Message sent.');
102123
});
103-
conn
104-
.subscriptionBuilder()
105-
.onApplied(() => {
106-
console.log('SDK client cache initialized.');
107-
})
108-
.subscribe(['SELECT * FROM message', 'SELECT * FROM user']);
124+
125+
subscribeToQueries(conn, ['SELECT * FROM message', 'SELECT * FROM user']);
109126
};
110127

111128
const onDisconnect = () => {
112129
console.log('Disconnected from SpacetimeDB');
113130
setConnected(false);
114131
};
115132

116-
const onConnectError = (_conn: DBConnection, err: Error) => {
133+
const onConnectError = (_ctx: ErrorContext, err: Error) => {
117134
console.log('Error connecting to SpacetimeDB:', err);
118135
};
119136

examples/quickstart-chat/src/module_bindings/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,16 @@ export class DBConnection extends DBConnectionImpl<
224224
RemoteReducers,
225225
SetReducerFlags
226226
> {
227-
static builder = (): DBConnectionBuilder<DBConnection> => {
228-
return new DBConnectionBuilder<DBConnection>(
229-
REMOTE_MODULE,
230-
(imp: DBConnectionImpl) => imp as DBConnection
231-
);
227+
static builder = (): DBConnectionBuilder<
228+
DBConnection,
229+
ErrorContext,
230+
SubscriptionEventContex
231+
> => {
232+
return new DBConnectionBuilder<
233+
DBConnection,
234+
ErrorContext,
235+
SubscriptionEventContex
236+
>(REMOTE_MODULE, (imp: DBConnectionImpl) => imp as DBConnection);
232237
};
233238
subscriptionBuilder = (): SubscriptionBuilder => {
234239
return new SubscriptionBuilder(this);

packages/sdk/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const connection = DBConnection.builder()
4040
identity.toHexString()
4141
);
4242

43-
connection.subscriptionBuilder().subscribe(['SELECT * FROM player']);
43+
connection.subscriptionBuilder().subscribe('SELECT * FROM player');
4444
})
4545
.withToken('TOKEN')
4646
.build();

packages/sdk/src/algebraic_type.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { TimeDuration, Timestamp } from '.';
12
import { ConnectionId } from './connection_id';
23
import type BinaryReader from './binary_reader';
34
import type BinaryWriter from './binary_writer';
@@ -162,9 +163,17 @@ export class ProductType {
162163
}
163164
};
164165

165-
deserialize = (reader: BinaryReader): object => {
166+
deserialize = (reader: BinaryReader): any => {
166167
let result: { [key: string]: any } = {};
167168
if (this.elements.length === 1) {
169+
if (this.elements[0].name === '__time_duration_micros__') {
170+
return new TimeDuration(reader.readI64());
171+
}
172+
173+
if (this.elements[0].name === '__timestamp_micros_since_unix_epoch__') {
174+
return new Timestamp(reader.readI64());
175+
}
176+
168177
if (this.elements[0].name === '__identity__') {
169178
return new Identity(reader.readU256());
170179
}
@@ -347,28 +356,29 @@ export class AlgebraicType {
347356
new ProductTypeElement('__identity__', this.createU256Type()),
348357
]);
349358
}
359+
350360
static createConnectionIdType(): AlgebraicType {
351361
return this.createProductType([
352362
new ProductTypeElement('__connection_id__', this.createU128Type()),
353363
]);
354364
}
365+
355366
static createScheduleAtType(): AlgebraicType {
356367
return ScheduleAt.getAlgebraicType();
357368
}
369+
358370
static createTimestampType(): AlgebraicType {
359-
return AlgebraicType.createProductType([
371+
return this.createProductType([
360372
new ProductTypeElement(
361373
'__timestamp_micros_since_unix_epoch__',
362-
AlgebraicType.createI64Type()
374+
this.createI64Type()
363375
),
364376
]);
365377
}
378+
366379
static createTimeDurationType(): AlgebraicType {
367-
return AlgebraicType.createProductType([
368-
new ProductTypeElement(
369-
'__time_duration_micros__',
370-
AlgebraicType.createI64Type()
371-
),
380+
return this.createProductType([
381+
new ProductTypeElement('__time_duration_micros__', this.createI64Type()),
372382
]);
373383
}
374384

packages/sdk/src/client_api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export { Unsubscribe };
8989
import { UnsubscribeApplied } from './unsubscribe_applied_type.ts';
9090
export { UnsubscribeApplied };
9191
import { UpdateStatus } from './update_status_type.ts';
92-
export { UpdateStatus };
92+
export { UpdateStatus, Timestamp };
9393

9494
const REMOTE_MODULE = {
9595
tables: {},

0 commit comments

Comments
 (0)