Skip to content

Commit 76dd3ef

Browse files
committed
Add reconnect impl for ws client
1 parent 1aab6cb commit 76dd3ef

File tree

1 file changed

+36
-0
lines changed
  • packages/libsql-client/src

1 file changed

+36
-0
lines changed

packages/libsql-client/src/ws.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,35 @@ export class WsClient implements Client {
403403
}
404404
}
405405

406+
async reconnect(): Promise<void> {
407+
try {
408+
for (const st of Array.from(this.#connState.streamStates)) {
409+
try {
410+
st.stream.close();
411+
} catch {}
412+
}
413+
this.#connState.client.close();
414+
} catch {}
415+
416+
if (this.#futureConnState) {
417+
try {
418+
this.#futureConnState.client.close();
419+
} catch {}
420+
this.#futureConnState = undefined;
421+
}
422+
423+
const next = this.#openConn();
424+
const version = await next.client.getVersion();
425+
426+
next.useSqlCache = version >= 2;
427+
if (next.useSqlCache) {
428+
next.sqlCache.capacity = sqlCacheCapacity;
429+
}
430+
431+
this.#connState = next;
432+
this.closed = false;
433+
}
434+
406435
_closeStream(streamState: StreamState): void {
407436
streamState.stream.close();
408437

@@ -421,6 +450,13 @@ export class WsClient implements Client {
421450
close(): void {
422451
this.#connState.client.close();
423452
this.closed = true;
453+
if (this.#futureConnState) {
454+
try {
455+
this.#futureConnState.client.close();
456+
} catch {}
457+
this.#futureConnState = undefined;
458+
}
459+
this.closed = true;
424460
}
425461
}
426462

0 commit comments

Comments
 (0)