Skip to content

Commit a501f5c

Browse files
CentrilPuruVJ
andauthored
Companion to SpacetimeDB#1812 (light tx, ts) (#110)
* support TransactionUpdateLight and CallReducerFlags * Run generate * Push creds * run prettier * Add changeset * Turn on withLightMode --------- Co-authored-by: Puru Vijay <awesomepuruvj@gmail.com> Co-authored-by: Puru Vijay <47742487+PuruVJ@users.noreply.github.com>
1 parent 6cee7f4 commit a501f5c

21 files changed

+328
-44
lines changed

.changeset/long-laws-mix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clockworklabs/spacetimedb-sdk': minor
3+
---
4+
5+
Support light tx updates via builder.with*light_mode(*) and the call flag NoSuccessNotify

examples/quickstart/src/module_bindings/index.ts

+65-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
// @ts-ignore
1414
BinaryWriter,
1515
// @ts-ignore
16+
CallReducerFlags,
17+
// @ts-ignore
1618
DBConnectionBuilder,
1719
// @ts-ignore
1820
DBConnectionImpl,
@@ -105,8 +107,14 @@ const REMOTE_MODULE = {
105107
dbViewConstructor: (imp: DBConnectionImpl) => {
106108
return new RemoteTables(imp);
107109
},
108-
reducersConstructor: (imp: DBConnectionImpl) => {
109-
return new RemoteReducers(imp);
110+
reducersConstructor: (
111+
imp: DBConnectionImpl,
112+
setReducerFlags: SetReducerFlags
113+
) => {
114+
return new RemoteReducers(imp, setReducerFlags);
115+
},
116+
setReducerFlagsConstructor: () => {
117+
return new SetReducerFlags();
110118
},
111119
};
112120

@@ -120,10 +128,17 @@ export type Reducer =
120128
| { name: 'SetName'; args: SetName };
121129

122130
export class RemoteReducers {
123-
constructor(private connection: DBConnectionImpl) {}
131+
constructor(
132+
private connection: DBConnectionImpl,
133+
private setCallReducerFlags: SetReducerFlags
134+
) {}
124135

125136
identityConnected() {
126-
this.connection.callReducer('__identity_connected__', new Uint8Array(0));
137+
this.connection.callReducer(
138+
'__identity_connected__',
139+
new Uint8Array(0),
140+
this.setCallReducerFlags.identityConnectedFlags
141+
);
127142
}
128143

129144
onIdentityConnected(callback: (ctx: EventContext) => void) {
@@ -135,7 +150,11 @@ export class RemoteReducers {
135150
}
136151

137152
identityDisconnected() {
138-
this.connection.callReducer('__identity_disconnected__', new Uint8Array(0));
153+
this.connection.callReducer(
154+
'__identity_disconnected__',
155+
new Uint8Array(0),
156+
this.setCallReducerFlags.identityDisconnectedFlags
157+
);
139158
}
140159

141160
onIdentityDisconnected(callback: (ctx: EventContext) => void) {
@@ -147,7 +166,11 @@ export class RemoteReducers {
147166
}
148167

149168
init() {
150-
this.connection.callReducer('__init__', new Uint8Array(0));
169+
this.connection.callReducer(
170+
'__init__',
171+
new Uint8Array(0),
172+
this.setCallReducerFlags.initFlags
173+
);
151174
}
152175

153176
onInit(callback: (ctx: EventContext) => void) {
@@ -163,7 +186,11 @@ export class RemoteReducers {
163186
let __writer = new BinaryWriter(1024);
164187
SendMessage.getAlgebraicType().serialize(__writer, __args);
165188
let __argsBuffer = __writer.getBuffer();
166-
this.connection.callReducer('send_message', __argsBuffer);
189+
this.connection.callReducer(
190+
'send_message',
191+
__argsBuffer,
192+
this.setCallReducerFlags.sendMessageFlags
193+
);
167194
}
168195

169196
onSendMessage(callback: (ctx: EventContext, text: string) => void) {
@@ -179,7 +206,11 @@ export class RemoteReducers {
179206
let __writer = new BinaryWriter(1024);
180207
SetName.getAlgebraicType().serialize(__writer, __args);
181208
let __argsBuffer = __writer.getBuffer();
182-
this.connection.callReducer('set_name', __argsBuffer);
209+
this.connection.callReducer(
210+
'set_name',
211+
__argsBuffer,
212+
this.setCallReducerFlags.setNameFlags
213+
);
183214
}
184215

185216
onSetName(callback: (ctx: EventContext, name: string) => void) {
@@ -191,6 +222,29 @@ export class RemoteReducers {
191222
}
192223
}
193224

225+
export class SetReducerFlags {
226+
identityConnectedFlags: CallReducerFlags;
227+
identityConnected(flags: CallReducerFlags) {
228+
this.identityConnectedFlags = flags;
229+
}
230+
identityDisconnectedFlags: CallReducerFlags;
231+
identityDisconnected(flags: CallReducerFlags) {
232+
this.identityDisconnectedFlags = flags;
233+
}
234+
initFlags: CallReducerFlags;
235+
init(flags: CallReducerFlags) {
236+
this.initFlags = flags;
237+
}
238+
sendMessageFlags: CallReducerFlags;
239+
sendMessage(flags: CallReducerFlags) {
240+
this.sendMessageFlags = flags;
241+
}
242+
setNameFlags: CallReducerFlags;
243+
setName(flags: CallReducerFlags) {
244+
this.setNameFlags = flags;
245+
}
246+
}
247+
194248
export class RemoteTables {
195249
constructor(private connection: DBConnectionImpl) {}
196250

@@ -211,7 +265,8 @@ export class RemoteTables {
211265

212266
export class DBConnection extends DBConnectionImpl<
213267
RemoteTables,
214-
RemoteReducers
268+
RemoteReducers,
269+
SetReducerFlags
215270
> {
216271
static builder = (): DBConnectionBuilder<DBConnection> => {
217272
return new DBConnectionBuilder<DBConnection>(
@@ -224,5 +279,6 @@ export class DBConnection extends DBConnectionImpl<
224279
export type EventContext = EventContextInterface<
225280
RemoteTables,
226281
RemoteReducers,
282+
SetReducerFlags,
227283
Reducer
228284
>;

packages/sdk/src/client_api/call_reducer_type.ts

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type CallReducer = {
4141
reducer: string;
4242
args: Uint8Array;
4343
requestId: number;
44+
flags: number;
4445
};
4546

4647
/**
@@ -59,6 +60,7 @@ export namespace CallReducer {
5960
AlgebraicType.createArrayType(AlgebraicType.createU8Type())
6061
),
6162
new ProductTypeElement('requestId', AlgebraicType.createU32Type()),
63+
new ProductTypeElement('flags', AlgebraicType.createU8Type()),
6264
]);
6365
}
6466

packages/sdk/src/client_api/index.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
// @ts-ignore
1414
BinaryWriter,
1515
// @ts-ignore
16+
CallReducerFlags,
17+
// @ts-ignore
1618
DBConnectionBuilder,
1719
// @ts-ignore
1820
DBConnectionImpl,
@@ -81,6 +83,8 @@ import { Timestamp } from './timestamp_type.ts';
8183
export { Timestamp };
8284
import { TransactionUpdate } from './transaction_update_type.ts';
8385
export { TransactionUpdate };
86+
import { TransactionUpdateLight } from './transaction_update_light_type.ts';
87+
export { TransactionUpdateLight };
8488
import { UpdateStatus } from './update_status_type.ts';
8589
export { UpdateStatus };
8690

@@ -98,25 +102,37 @@ const REMOTE_MODULE = {
98102
dbViewConstructor: (imp: DBConnectionImpl) => {
99103
return new RemoteTables(imp);
100104
},
101-
reducersConstructor: (imp: DBConnectionImpl) => {
102-
return new RemoteReducers(imp);
105+
reducersConstructor: (
106+
imp: DBConnectionImpl,
107+
setReducerFlags: SetReducerFlags
108+
) => {
109+
return new RemoteReducers(imp, setReducerFlags);
110+
},
111+
setReducerFlagsConstructor: () => {
112+
return new SetReducerFlags();
103113
},
104114
};
105115

106116
// A type representing all the possible variants of a reducer.
107117
export type Reducer = never;
108118

109119
export class RemoteReducers {
110-
constructor(private connection: DBConnectionImpl) {}
120+
constructor(
121+
private connection: DBConnectionImpl,
122+
private setCallReducerFlags: SetReducerFlags
123+
) {}
111124
}
112125

126+
export class SetReducerFlags {}
127+
113128
export class RemoteTables {
114129
constructor(private connection: DBConnectionImpl) {}
115130
}
116131

117132
export class DBConnection extends DBConnectionImpl<
118133
RemoteTables,
119-
RemoteReducers
134+
RemoteReducers,
135+
SetReducerFlags
120136
> {
121137
static builder = (): DBConnectionBuilder<DBConnection> => {
122138
return new DBConnectionBuilder<DBConnection>(
@@ -129,5 +145,6 @@ export class DBConnection extends DBConnectionImpl<
129145
export type EventContext = EventContextInterface<
130146
RemoteTables,
131147
RemoteReducers,
148+
SetReducerFlags,
132149
Reducer
133150
>;

packages/sdk/src/client_api/server_message_type.ts

+14
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import { InitialSubscription as __InitialSubscription } from './initial_subscrip
4242
// @ts-ignore
4343
import { TransactionUpdate as __TransactionUpdate } from './transaction_update_type';
4444
// @ts-ignore
45+
import { TransactionUpdateLight as __TransactionUpdateLight } from './transaction_update_light_type';
46+
// @ts-ignore
4547
import { IdentityToken as __IdentityToken } from './identity_token_type';
4648
// @ts-ignore
4749
import { OneOffQueryResponse as __OneOffQueryResponse } from './one_off_query_response_type';
@@ -59,6 +61,10 @@ export namespace ServerMessage {
5961
tag: 'TransactionUpdate';
6062
value: __TransactionUpdate;
6163
};
64+
export type TransactionUpdateLight = {
65+
tag: 'TransactionUpdateLight';
66+
value: __TransactionUpdateLight;
67+
};
6268
export type IdentityToken = { tag: 'IdentityToken'; value: __IdentityToken };
6369
export type OneOffQueryResponse = {
6470
tag: 'OneOffQueryResponse';
@@ -77,6 +83,9 @@ export namespace ServerMessage {
7783
export const TransactionUpdate = (
7884
value: __TransactionUpdate
7985
): ServerMessage => ({ tag: 'TransactionUpdate', value });
86+
export const TransactionUpdateLight = (
87+
value: __TransactionUpdateLight
88+
): ServerMessage => ({ tag: 'TransactionUpdateLight', value });
8089
export const IdentityToken = (value: __IdentityToken): ServerMessage => ({
8190
tag: 'IdentityToken',
8291
value,
@@ -95,6 +104,10 @@ export namespace ServerMessage {
95104
'TransactionUpdate',
96105
__TransactionUpdate.getTypeScriptAlgebraicType()
97106
),
107+
new SumTypeVariant(
108+
'TransactionUpdateLight',
109+
__TransactionUpdateLight.getTypeScriptAlgebraicType()
110+
),
98111
new SumTypeVariant(
99112
'IdentityToken',
100113
__IdentityToken.getTypeScriptAlgebraicType()
@@ -119,6 +132,7 @@ export namespace ServerMessage {
119132
export type ServerMessage =
120133
| ServerMessage.InitialSubscription
121134
| ServerMessage.TransactionUpdate
135+
| ServerMessage.TransactionUpdateLight
122136
| ServerMessage.IdentityToken
123137
| ServerMessage.OneOffQueryResponse;
124138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
2+
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
3+
4+
import {
5+
// @ts-ignore
6+
Address,
7+
// @ts-ignore
8+
AlgebraicType,
9+
// @ts-ignore
10+
AlgebraicValue,
11+
// @ts-ignore
12+
BinaryReader,
13+
// @ts-ignore
14+
BinaryWriter,
15+
// @ts-ignore
16+
DBConnectionBuilder,
17+
// @ts-ignore
18+
DBConnectionImpl,
19+
// @ts-ignore
20+
DBContext,
21+
// @ts-ignore
22+
Event,
23+
// @ts-ignore
24+
EventContextInterface,
25+
// @ts-ignore
26+
Identity,
27+
// @ts-ignore
28+
ProductType,
29+
// @ts-ignore
30+
ProductTypeElement,
31+
// @ts-ignore
32+
SumType,
33+
// @ts-ignore
34+
SumTypeVariant,
35+
// @ts-ignore
36+
TableCache,
37+
// @ts-ignore
38+
deepEqual,
39+
} from '..';
40+
// @ts-ignore
41+
import { DatabaseUpdate as __DatabaseUpdate } from './database_update_type';
42+
43+
export type TransactionUpdateLight = {
44+
requestId: number;
45+
update: __DatabaseUpdate;
46+
};
47+
48+
/**
49+
* A namespace for generated helper functions.
50+
*/
51+
export namespace TransactionUpdateLight {
52+
/**
53+
* A function which returns this type represented as an AlgebraicType.
54+
* This function is derived from the AlgebraicType used to generate this type.
55+
*/
56+
export function getTypeScriptAlgebraicType(): AlgebraicType {
57+
return AlgebraicType.createProductType([
58+
new ProductTypeElement('requestId', AlgebraicType.createU32Type()),
59+
new ProductTypeElement(
60+
'update',
61+
__DatabaseUpdate.getTypeScriptAlgebraicType()
62+
),
63+
]);
64+
}
65+
66+
export function serialize(
67+
writer: BinaryWriter,
68+
value: TransactionUpdateLight
69+
): void {
70+
TransactionUpdateLight.getTypeScriptAlgebraicType().serialize(
71+
writer,
72+
value
73+
);
74+
}
75+
76+
export function deserialize(reader: BinaryReader): TransactionUpdateLight {
77+
return TransactionUpdateLight.getTypeScriptAlgebraicType().deserialize(
78+
reader
79+
);
80+
}
81+
}

packages/sdk/src/db_connection_builder.ts

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class DBConnectionBuilder<DBConnection> {
1616
#emitter: EventEmitter = new EventEmitter();
1717
#createWSFn: typeof WebsocketDecompressAdapter.createWebSocketFn;
1818
#compression: 'gzip' | 'none' = 'gzip';
19+
#light_mode: boolean = false;
1920

2021
/**
2122
* Creates a new `SpacetimeDBClient` database client and set the initial parameters.
@@ -78,6 +79,11 @@ export class DBConnectionBuilder<DBConnection> {
7879
return this;
7980
}
8081

82+
withLightMode(light_mode: boolean): DBConnectionBuilder<DBConnection> {
83+
this.#light_mode = light_mode;
84+
return this;
85+
}
86+
8187
/**
8288
* Connect to The SpacetimeDB Websocket For Your Module. By default, this will use a secure websocket connection. The parameters are optional, and if not provided, will use the values provided on construction of the client.
8389
*
@@ -127,6 +133,7 @@ export class DBConnectionBuilder<DBConnection> {
127133
wsProtocol: 'v1.bsatn.spacetimedb',
128134
authToken: connection.token,
129135
compression: this.#compression,
136+
light_mode: this.#light_mode,
130137
})
131138
.then(v => {
132139
connection.ws = v;

0 commit comments

Comments
 (0)