Skip to content

Commit 6f36e48

Browse files
authored
TypeScript: Add confirmed reads config (#3247)
# Description of Changes Adds a way to configure the DbConnection to use confirmed reads. # Expected complexity level and risk 1 # Testing Still figuring out...
1 parent ad01a9e commit 6f36e48

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

crates/bindings-typescript/src/sdk/db_connection_builder.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class DbConnectionBuilder<
2020
#emitter: EventEmitter<ConnectionEvent> = new EventEmitter();
2121
#compression: 'gzip' | 'none' = 'gzip';
2222
#lightMode: boolean = false;
23+
#confirmedReads?: boolean;
2324
#createWSFn: typeof WebsocketDecompressAdapter.createWebSocketFn;
2425

2526
/**
@@ -107,6 +108,31 @@ export class DbConnectionBuilder<
107108
return this;
108109
}
109110

111+
/**
112+
* Sets the connection to use confirmed reads.
113+
*
114+
* When enabled, the server will send query results only after they are
115+
* confirmed to be durable.
116+
*
117+
* What durable means depends on the server configuration: a single node
118+
* server may consider a transaction durable once it is `fsync`'ed to disk,
119+
* whereas a cluster may require that some number of replicas have
120+
* acknowledge that they have stored the transactions.
121+
*
122+
* Note that enabling confirmed reads will increase the latency between a
123+
* reducer call and the corresponding subscription update arriving at the
124+
* client.
125+
*
126+
* If this method is not called, not preference is sent to the server, and
127+
* the server will choose the default.
128+
*
129+
* @param confirmedReads `true` to enable confirmed reads, `false` to disable.
130+
*/
131+
withConfirmedReads(confirmedReads: boolean): this {
132+
this.#confirmedReads = confirmedReads;
133+
return this;
134+
}
135+
110136
/**
111137
* Register a callback to be invoked upon authentication with the database.
112138
*
@@ -228,6 +254,7 @@ export class DbConnectionBuilder<
228254
emitter: this.#emitter,
229255
compression: this.#compression,
230256
lightMode: this.#lightMode,
257+
confirmedReads: this.#confirmedReads,
231258
createWSFn: this.#createWSFn,
232259
remoteModule: this.remoteModule,
233260
})

crates/bindings-typescript/src/sdk/db_connection_impl.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ type DbConnectionConfig = {
100100
createWSFn: typeof WebsocketDecompressAdapter.createWebSocketFn;
101101
compression: 'gzip' | 'none';
102102
lightMode: boolean;
103+
confirmedReads?: boolean;
103104
};
104105

105106
export class DbConnectionImpl<
@@ -177,6 +178,7 @@ export class DbConnectionImpl<
177178
createWSFn,
178179
compression,
179180
lightMode,
181+
confirmedReads,
180182
}: DbConnectionConfig) {
181183
stdbLogger('info', 'Connecting to SpacetimeDB WS...');
182184

@@ -212,6 +214,7 @@ export class DbConnectionImpl<
212214
authToken: token,
213215
compression: compression,
214216
lightMode: lightMode,
217+
confirmedReads: confirmedReads,
215218
})
216219
.then(v => {
217220
this.ws = v;

crates/bindings-typescript/src/sdk/websocket_decompress_adapter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@ export class WebsocketDecompressAdapter {
7373
authToken,
7474
compression,
7575
lightMode,
76+
confirmedReads,
7677
}: {
7778
url: URL;
7879
wsProtocol: string;
7980
nameOrAddress: string;
8081
authToken?: string;
8182
compression: 'gzip' | 'none';
8283
lightMode: boolean;
84+
confirmedReads?: boolean;
8385
}): Promise<WebsocketDecompressAdapter> {
8486
const headers = new Headers();
8587

@@ -115,6 +117,9 @@ export class WebsocketDecompressAdapter {
115117
if (lightMode) {
116118
databaseUrl.searchParams.set('light', 'true');
117119
}
120+
if (confirmedReads !== undefined) {
121+
databaseUrl.searchParams.set('confirmed', confirmedReads.toString());
122+
}
118123

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

0 commit comments

Comments
 (0)