Skip to content

Commit

Permalink
Companion to SpacetimeDB#1812 (light tx, ts) (#110)
Browse files Browse the repository at this point in the history
* support TransactionUpdateLight and CallReducerFlags

* Run generate

* Push creds

* run prettier

* Add changeset

* Turn on withLightMode

---------

Co-authored-by: Puru Vijay <[email protected]>
Co-authored-by: Puru Vijay <[email protected]>
  • Loading branch information
3 people authored Nov 6, 2024
1 parent 6cee7f4 commit a501f5c
Show file tree
Hide file tree
Showing 21 changed files with 328 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-laws-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clockworklabs/spacetimedb-sdk': minor
---

Support light tx updates via builder.with*light_mode(*) and the call flag NoSuccessNotify
74 changes: 65 additions & 9 deletions examples/quickstart/src/module_bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
// @ts-ignore
BinaryWriter,
// @ts-ignore
CallReducerFlags,
// @ts-ignore
DBConnectionBuilder,
// @ts-ignore
DBConnectionImpl,
Expand Down Expand Up @@ -105,8 +107,14 @@ const REMOTE_MODULE = {
dbViewConstructor: (imp: DBConnectionImpl) => {
return new RemoteTables(imp);
},
reducersConstructor: (imp: DBConnectionImpl) => {
return new RemoteReducers(imp);
reducersConstructor: (
imp: DBConnectionImpl,
setReducerFlags: SetReducerFlags
) => {
return new RemoteReducers(imp, setReducerFlags);
},
setReducerFlagsConstructor: () => {
return new SetReducerFlags();
},
};

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

export class RemoteReducers {
constructor(private connection: DBConnectionImpl) {}
constructor(
private connection: DBConnectionImpl,
private setCallReducerFlags: SetReducerFlags
) {}

identityConnected() {
this.connection.callReducer('__identity_connected__', new Uint8Array(0));
this.connection.callReducer(
'__identity_connected__',
new Uint8Array(0),
this.setCallReducerFlags.identityConnectedFlags
);
}

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

identityDisconnected() {
this.connection.callReducer('__identity_disconnected__', new Uint8Array(0));
this.connection.callReducer(
'__identity_disconnected__',
new Uint8Array(0),
this.setCallReducerFlags.identityDisconnectedFlags
);
}

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

init() {
this.connection.callReducer('__init__', new Uint8Array(0));
this.connection.callReducer(
'__init__',
new Uint8Array(0),
this.setCallReducerFlags.initFlags
);
}

onInit(callback: (ctx: EventContext) => void) {
Expand All @@ -163,7 +186,11 @@ export class RemoteReducers {
let __writer = new BinaryWriter(1024);
SendMessage.getAlgebraicType().serialize(__writer, __args);
let __argsBuffer = __writer.getBuffer();
this.connection.callReducer('send_message', __argsBuffer);
this.connection.callReducer(
'send_message',
__argsBuffer,
this.setCallReducerFlags.sendMessageFlags
);
}

onSendMessage(callback: (ctx: EventContext, text: string) => void) {
Expand All @@ -179,7 +206,11 @@ export class RemoteReducers {
let __writer = new BinaryWriter(1024);
SetName.getAlgebraicType().serialize(__writer, __args);
let __argsBuffer = __writer.getBuffer();
this.connection.callReducer('set_name', __argsBuffer);
this.connection.callReducer(
'set_name',
__argsBuffer,
this.setCallReducerFlags.setNameFlags
);
}

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

export class SetReducerFlags {
identityConnectedFlags: CallReducerFlags;
identityConnected(flags: CallReducerFlags) {
this.identityConnectedFlags = flags;
}
identityDisconnectedFlags: CallReducerFlags;
identityDisconnected(flags: CallReducerFlags) {
this.identityDisconnectedFlags = flags;
}
initFlags: CallReducerFlags;
init(flags: CallReducerFlags) {
this.initFlags = flags;
}
sendMessageFlags: CallReducerFlags;
sendMessage(flags: CallReducerFlags) {
this.sendMessageFlags = flags;
}
setNameFlags: CallReducerFlags;
setName(flags: CallReducerFlags) {
this.setNameFlags = flags;
}
}

export class RemoteTables {
constructor(private connection: DBConnectionImpl) {}

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

export class DBConnection extends DBConnectionImpl<
RemoteTables,
RemoteReducers
RemoteReducers,
SetReducerFlags
> {
static builder = (): DBConnectionBuilder<DBConnection> => {
return new DBConnectionBuilder<DBConnection>(
Expand All @@ -224,5 +279,6 @@ export class DBConnection extends DBConnectionImpl<
export type EventContext = EventContextInterface<
RemoteTables,
RemoteReducers,
SetReducerFlags,
Reducer
>;
2 changes: 2 additions & 0 deletions packages/sdk/src/client_api/call_reducer_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type CallReducer = {
reducer: string;
args: Uint8Array;
requestId: number;
flags: number;
};

/**
Expand All @@ -59,6 +60,7 @@ export namespace CallReducer {
AlgebraicType.createArrayType(AlgebraicType.createU8Type())
),
new ProductTypeElement('requestId', AlgebraicType.createU32Type()),
new ProductTypeElement('flags', AlgebraicType.createU8Type()),
]);
}

Expand Down
25 changes: 21 additions & 4 deletions packages/sdk/src/client_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
// @ts-ignore
BinaryWriter,
// @ts-ignore
CallReducerFlags,
// @ts-ignore
DBConnectionBuilder,
// @ts-ignore
DBConnectionImpl,
Expand Down Expand Up @@ -81,6 +83,8 @@ import { Timestamp } from './timestamp_type.ts';
export { Timestamp };
import { TransactionUpdate } from './transaction_update_type.ts';
export { TransactionUpdate };
import { TransactionUpdateLight } from './transaction_update_light_type.ts';
export { TransactionUpdateLight };
import { UpdateStatus } from './update_status_type.ts';
export { UpdateStatus };

Expand All @@ -98,25 +102,37 @@ const REMOTE_MODULE = {
dbViewConstructor: (imp: DBConnectionImpl) => {
return new RemoteTables(imp);
},
reducersConstructor: (imp: DBConnectionImpl) => {
return new RemoteReducers(imp);
reducersConstructor: (
imp: DBConnectionImpl,
setReducerFlags: SetReducerFlags
) => {
return new RemoteReducers(imp, setReducerFlags);
},
setReducerFlagsConstructor: () => {
return new SetReducerFlags();
},
};

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

export class RemoteReducers {
constructor(private connection: DBConnectionImpl) {}
constructor(
private connection: DBConnectionImpl,
private setCallReducerFlags: SetReducerFlags
) {}
}

export class SetReducerFlags {}

export class RemoteTables {
constructor(private connection: DBConnectionImpl) {}
}

export class DBConnection extends DBConnectionImpl<
RemoteTables,
RemoteReducers
RemoteReducers,
SetReducerFlags
> {
static builder = (): DBConnectionBuilder<DBConnection> => {
return new DBConnectionBuilder<DBConnection>(
Expand All @@ -129,5 +145,6 @@ export class DBConnection extends DBConnectionImpl<
export type EventContext = EventContextInterface<
RemoteTables,
RemoteReducers,
SetReducerFlags,
Reducer
>;
14 changes: 14 additions & 0 deletions packages/sdk/src/client_api/server_message_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import { InitialSubscription as __InitialSubscription } from './initial_subscrip
// @ts-ignore
import { TransactionUpdate as __TransactionUpdate } from './transaction_update_type';
// @ts-ignore
import { TransactionUpdateLight as __TransactionUpdateLight } from './transaction_update_light_type';
// @ts-ignore
import { IdentityToken as __IdentityToken } from './identity_token_type';
// @ts-ignore
import { OneOffQueryResponse as __OneOffQueryResponse } from './one_off_query_response_type';
Expand All @@ -59,6 +61,10 @@ export namespace ServerMessage {
tag: 'TransactionUpdate';
value: __TransactionUpdate;
};
export type TransactionUpdateLight = {
tag: 'TransactionUpdateLight';
value: __TransactionUpdateLight;
};
export type IdentityToken = { tag: 'IdentityToken'; value: __IdentityToken };
export type OneOffQueryResponse = {
tag: 'OneOffQueryResponse';
Expand All @@ -77,6 +83,9 @@ export namespace ServerMessage {
export const TransactionUpdate = (
value: __TransactionUpdate
): ServerMessage => ({ tag: 'TransactionUpdate', value });
export const TransactionUpdateLight = (
value: __TransactionUpdateLight
): ServerMessage => ({ tag: 'TransactionUpdateLight', value });
export const IdentityToken = (value: __IdentityToken): ServerMessage => ({
tag: 'IdentityToken',
value,
Expand All @@ -95,6 +104,10 @@ export namespace ServerMessage {
'TransactionUpdate',
__TransactionUpdate.getTypeScriptAlgebraicType()
),
new SumTypeVariant(
'TransactionUpdateLight',
__TransactionUpdateLight.getTypeScriptAlgebraicType()
),
new SumTypeVariant(
'IdentityToken',
__IdentityToken.getTypeScriptAlgebraicType()
Expand All @@ -119,6 +132,7 @@ export namespace ServerMessage {
export type ServerMessage =
| ServerMessage.InitialSubscription
| ServerMessage.TransactionUpdate
| ServerMessage.TransactionUpdateLight
| ServerMessage.IdentityToken
| ServerMessage.OneOffQueryResponse;

Expand Down
81 changes: 81 additions & 0 deletions packages/sdk/src/client_api/transaction_update_light_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

import {
// @ts-ignore
Address,
// @ts-ignore
AlgebraicType,
// @ts-ignore
AlgebraicValue,
// @ts-ignore
BinaryReader,
// @ts-ignore
BinaryWriter,
// @ts-ignore
DBConnectionBuilder,
// @ts-ignore
DBConnectionImpl,
// @ts-ignore
DBContext,
// @ts-ignore
Event,
// @ts-ignore
EventContextInterface,
// @ts-ignore
Identity,
// @ts-ignore
ProductType,
// @ts-ignore
ProductTypeElement,
// @ts-ignore
SumType,
// @ts-ignore
SumTypeVariant,
// @ts-ignore
TableCache,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { DatabaseUpdate as __DatabaseUpdate } from './database_update_type';

export type TransactionUpdateLight = {
requestId: number;
update: __DatabaseUpdate;
};

/**
* A namespace for generated helper functions.
*/
export namespace TransactionUpdateLight {
/**
* A function which returns this type represented as an AlgebraicType.
* This function is derived from the AlgebraicType used to generate this type.
*/
export function getTypeScriptAlgebraicType(): AlgebraicType {
return AlgebraicType.createProductType([
new ProductTypeElement('requestId', AlgebraicType.createU32Type()),
new ProductTypeElement(
'update',
__DatabaseUpdate.getTypeScriptAlgebraicType()
),
]);
}

export function serialize(
writer: BinaryWriter,
value: TransactionUpdateLight
): void {
TransactionUpdateLight.getTypeScriptAlgebraicType().serialize(
writer,
value
);
}

export function deserialize(reader: BinaryReader): TransactionUpdateLight {
return TransactionUpdateLight.getTypeScriptAlgebraicType().deserialize(
reader
);
}
}
7 changes: 7 additions & 0 deletions packages/sdk/src/db_connection_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class DBConnectionBuilder<DBConnection> {
#emitter: EventEmitter = new EventEmitter();
#createWSFn: typeof WebsocketDecompressAdapter.createWebSocketFn;
#compression: 'gzip' | 'none' = 'gzip';
#light_mode: boolean = false;

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

withLightMode(light_mode: boolean): DBConnectionBuilder<DBConnection> {
this.#light_mode = light_mode;
return this;
}

/**
* 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.
*
Expand Down Expand Up @@ -127,6 +133,7 @@ export class DBConnectionBuilder<DBConnection> {
wsProtocol: 'v1.bsatn.spacetimedb',
authToken: connection.token,
compression: this.#compression,
light_mode: this.#light_mode,
})
.then(v => {
connection.ws = v;
Expand Down
Loading

0 comments on commit a501f5c

Please sign in to comment.