Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions crates/bindings-typescript/src/sdk/db_connection_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class DbConnectionBuilder<
#emitter: EventEmitter<ConnectionEvent> = new EventEmitter();
#compression: 'gzip' | 'none' = 'gzip';
#lightMode: boolean = false;
#confirmedReads?: boolean;
#createWSFn: typeof WebsocketDecompressAdapter.createWebSocketFn;

/**
Expand Down Expand Up @@ -107,6 +108,31 @@ export class DbConnectionBuilder<
return this;
}

/**
* Sets the connection to use confirmed reads.
*
* When enabled, the server will send query results only after they are
* confirmed to be durable.
*
* What durable means depends on the server configuration: a single node
* server may consider a transaction durable once it is `fsync`'ed to disk,
* whereas a cluster may require that some number of replicas have
* acknowledge that they have stored the transactions.
*
* Note that enabling confirmed reads will increase the latency between a
* reducer call and the corresponding subscription update arriving at the
* client.
*
* If this method is not called, not preference is sent to the server, and
* the server will choose the default.
*
* @param confirmedReads `true` to enable confirmed reads, `false` to disable.
*/
withConfirmedReads(confirmedReads: boolean): this {
this.#confirmedReads = confirmedReads;
return this;
}

/**
* Register a callback to be invoked upon authentication with the database.
*
Expand Down Expand Up @@ -228,6 +254,7 @@ export class DbConnectionBuilder<
emitter: this.#emitter,
compression: this.#compression,
lightMode: this.#lightMode,
confirmedReads: this.#confirmedReads,
createWSFn: this.#createWSFn,
remoteModule: this.remoteModule,
})
Expand Down
3 changes: 3 additions & 0 deletions crates/bindings-typescript/src/sdk/db_connection_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type DbConnectionConfig = {
createWSFn: typeof WebsocketDecompressAdapter.createWebSocketFn;
compression: 'gzip' | 'none';
lightMode: boolean;
confirmedReads?: boolean;
};

export class DbConnectionImpl<
Expand Down Expand Up @@ -177,6 +178,7 @@ export class DbConnectionImpl<
createWSFn,
compression,
lightMode,
confirmedReads,
}: DbConnectionConfig) {
stdbLogger('info', 'Connecting to SpacetimeDB WS...');

Expand Down Expand Up @@ -212,6 +214,7 @@ export class DbConnectionImpl<
authToken: token,
compression: compression,
lightMode: lightMode,
confirmedReads: confirmedReads,
})
.then(v => {
this.ws = v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ export class WebsocketDecompressAdapter {
authToken,
compression,
lightMode,
confirmedReads,
}: {
url: URL;
wsProtocol: string;
nameOrAddress: string;
authToken?: string;
compression: 'gzip' | 'none';
lightMode: boolean;
confirmedReads?: boolean;
}): Promise<WebsocketDecompressAdapter> {
const headers = new Headers();

Expand Down Expand Up @@ -115,6 +117,9 @@ export class WebsocketDecompressAdapter {
if (lightMode) {
databaseUrl.searchParams.set('light', 'true');
}
if (confirmedReads !== undefined) {
databaseUrl.searchParams.set('confirmed', confirmedReads.toString());
}

const ws = new WS(databaseUrl.toString(), wsProtocol);

Expand Down
Loading