refactor[wip]: Connection pool logic.
Some checks failed
Build & Test / build (20.x) (push) Has been cancelled
Build & Test / build (22.x) (push) Has been cancelled

This commit is contained in:
Shibo Lyu 2025-06-09 21:23:27 +08:00
parent 747fcbbdb8
commit 28cd888612
4 changed files with 25 additions and 20 deletions

View file

@ -9,12 +9,14 @@ type SavedObject = {
encodedIdKeyPair?: EncodedBlahKeyPair; encodedIdKeyPair?: EncodedBlahKeyPair;
actKeyId: string; actKeyId: string;
actKeyPrivate: CryptoKey; actKeyPrivate: CryptoKey;
chatServers: string[];
}; };
export type AccountCredentials = { export type AccountCredentials = {
idKeyId: string; idKeyId: string;
encodedIdKeyPair?: EncodedBlahKeyPair; encodedIdKeyPair?: EncodedBlahKeyPair;
actKeyPair: BlahKeyPair; actKeyPair: BlahKeyPair;
chatServers: string[];
}; };
interface AccountKeyDBSchema extends DBSchema { interface AccountKeyDBSchema extends DBSchema {
@ -32,7 +34,8 @@ async function savedObjectToAccountCredentials(
return { return {
idKeyId: savedObject.idKeyId, idKeyId: savedObject.idKeyId,
encodedIdKeyPair: savedObject.encodedIdKeyPair, encodedIdKeyPair: savedObject.encodedIdKeyPair,
actKeyPair: new BlahKeyPair(publicKey, savedObject.actKeyPrivate) actKeyPair: new BlahKeyPair(publicKey, savedObject.actKeyPrivate),
chatServers: savedObject.chatServers
}; };
} }
@ -59,13 +62,15 @@ class AccountKeyDB {
async addAccount( async addAccount(
idKeyId: string, idKeyId: string,
actKeyPair: BlahKeyPair, actKeyPair: BlahKeyPair,
encodedIdKeyPair?: EncodedBlahKeyPair encodedIdKeyPair?: EncodedBlahKeyPair,
chatServers: string[] = []
): Promise<AccountCredentials> { ): Promise<AccountCredentials> {
const newObject: SavedObject = { const newObject: SavedObject = {
idKeyId, idKeyId,
encodedIdKeyPair, encodedIdKeyPair,
actKeyId: actKeyPair.id, actKeyId: actKeyPair.id,
actKeyPrivate: actKeyPair.privateKey actKeyPrivate: actKeyPair.privateKey,
chatServers
}; };
const tx = this.db.transaction(IDB_OBJECT_STORE_NAME, 'readwrite'); const tx = this.db.transaction(IDB_OBJECT_STORE_NAME, 'readwrite');
@ -73,7 +78,7 @@ class AccountKeyDB {
await tx.store.put({ ...currentObject, ...newObject }); await tx.store.put({ ...currentObject, ...newObject });
await tx.done; await tx.done;
return { idKeyId, encodedIdKeyPair, actKeyPair }; return { idKeyId, encodedIdKeyPair, actKeyPair, chatServers };
} }
async updateEncodedIdKeyPair( async updateEncodedIdKeyPair(

View file

@ -25,6 +25,7 @@ class AccountManager {
currentAccount: Account | null = $derived( currentAccount: Account | null = $derived(
this.accounts.find((account) => account.id_key === this.currentAccountId) ?? null this.accounts.find((account) => account.id_key === this.currentAccountId) ?? null
); );
#connectionPool: ChatServerConnectionPool;
constructor() { constructor() {
if (browser) { if (browser) {

View file

@ -138,7 +138,7 @@ export class BlahChatServerConnection {
} }
}; };
const response = await this.apiCall('POST', '/user/register', request, { await this.apiCall('POST', '/user/register', request, {
powDifficulty: data.register_challenge.pow.difficulty powDifficulty: data.register_challenge.pow.difficulty
}); });
} else { } else {
@ -186,6 +186,7 @@ export class BlahChatServerConnection {
} }
connect() { connect() {
this.tryRegisterIfNoyYet();
if (!this.webSocket && this.identity) this.webSocket = this.createWebSocket(); if (!this.webSocket && this.identity) this.webSocket = this.createWebSocket();
} }

View file

@ -1,29 +1,29 @@
import { persisted } from 'svelte-persisted-store';
import { get } from 'svelte/store'; import { get } from 'svelte/store';
import { BlahChatServerConnection } from './blah/connection/chatServer'; import { BlahChatServerConnection } from './blah/connection';
import { BlahKeyPair, type EncodedBlahKeyPair } from '@blah-im/core/crypto'; import { BlahKeyPair, type EncodedBlahKeyPair } from '@blah-im/core/crypto';
import { ChatListManager } from './chatList'; import { ChatListManager } from './chatList';
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { GlobalSearchManager } from './globalSearch'; import { GlobalSearchManager } from './globalSearch';
import type { BlahIdentity } from '@blah-im/core/identity';
export const chatServers = persisted<string[]>('weblah-chat-servers', ['https://blah.oxa.li/api']); export const defaultChatServers = ['https://blah.oxa.li/api'];
export class ChatServerConnectionPool {
#connections: Map<string, BlahChatServerConnection> = new Map();
#identity: BlahIdentity | null = null;
#chatServers: string[] = defaultChatServers;
class ChatServerConnectionPool {
private connections: Map<string, BlahChatServerConnection> = new Map();
private keypair: BlahKeyPair | null = null;
chatList: ChatListManager = new ChatListManager(); chatList: ChatListManager = new ChatListManager();
searchManager: GlobalSearchManager = new GlobalSearchManager(this.connections); searchManager: GlobalSearchManager = new GlobalSearchManager(this.connections);
constructor() { constructor(identity: BlahIdentity, chatServers: string[] = defaultChatServers) {
if (browser) { this.#identity = identity;
chatServers.subscribe(this.onChatServersChange.bind(this)); this.#chatServers = chatServers;
// currentKeyPair.subscribe(this.onKeyPairChange.bind(this));
}
} }
private createAndConnect(endpoint: string) { private createAndConnect(endpoint: string) {
const connection = new BlahChatServerConnection(endpoint, this.keypair); const connection = new BlahChatServerConnection(endpoint, this.#identity);
this.connections.set(endpoint, connection); this.#connections.set(endpoint, connection);
connection.connect(); connection.connect();
this.setupChatList(connection); this.setupChatList(connection);
} }
@ -84,5 +84,3 @@ class ChatServerConnectionPool {
return this.connections.get(endpoint) ?? null; return this.connections.get(endpoint) ?? null;
} }
} }
export const chatServerConnectionPool = new ChatServerConnectionPool();