mirror of
https://github.com/Blah-IM/Weblah.git
synced 2025-06-23 00:11:08 +00:00
refactor[wip]: Connection pool logic.
This commit is contained in:
parent
747fcbbdb8
commit
28cd888612
4 changed files with 25 additions and 20 deletions
|
@ -9,12 +9,14 @@ type SavedObject = {
|
|||
encodedIdKeyPair?: EncodedBlahKeyPair;
|
||||
actKeyId: string;
|
||||
actKeyPrivate: CryptoKey;
|
||||
chatServers: string[];
|
||||
};
|
||||
|
||||
export type AccountCredentials = {
|
||||
idKeyId: string;
|
||||
encodedIdKeyPair?: EncodedBlahKeyPair;
|
||||
actKeyPair: BlahKeyPair;
|
||||
chatServers: string[];
|
||||
};
|
||||
|
||||
interface AccountKeyDBSchema extends DBSchema {
|
||||
|
@ -32,7 +34,8 @@ async function savedObjectToAccountCredentials(
|
|||
return {
|
||||
idKeyId: savedObject.idKeyId,
|
||||
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(
|
||||
idKeyId: string,
|
||||
actKeyPair: BlahKeyPair,
|
||||
encodedIdKeyPair?: EncodedBlahKeyPair
|
||||
encodedIdKeyPair?: EncodedBlahKeyPair,
|
||||
chatServers: string[] = []
|
||||
): Promise<AccountCredentials> {
|
||||
const newObject: SavedObject = {
|
||||
idKeyId,
|
||||
encodedIdKeyPair,
|
||||
actKeyId: actKeyPair.id,
|
||||
actKeyPrivate: actKeyPair.privateKey
|
||||
actKeyPrivate: actKeyPair.privateKey,
|
||||
chatServers
|
||||
};
|
||||
|
||||
const tx = this.db.transaction(IDB_OBJECT_STORE_NAME, 'readwrite');
|
||||
|
@ -73,7 +78,7 @@ class AccountKeyDB {
|
|||
await tx.store.put({ ...currentObject, ...newObject });
|
||||
await tx.done;
|
||||
|
||||
return { idKeyId, encodedIdKeyPair, actKeyPair };
|
||||
return { idKeyId, encodedIdKeyPair, actKeyPair, chatServers };
|
||||
}
|
||||
|
||||
async updateEncodedIdKeyPair(
|
||||
|
|
|
@ -25,6 +25,7 @@ class AccountManager {
|
|||
currentAccount: Account | null = $derived(
|
||||
this.accounts.find((account) => account.id_key === this.currentAccountId) ?? null
|
||||
);
|
||||
#connectionPool: ChatServerConnectionPool;
|
||||
|
||||
constructor() {
|
||||
if (browser) {
|
||||
|
|
|
@ -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
|
||||
});
|
||||
} else {
|
||||
|
@ -186,6 +186,7 @@ export class BlahChatServerConnection {
|
|||
}
|
||||
|
||||
connect() {
|
||||
this.tryRegisterIfNoyYet();
|
||||
if (!this.webSocket && this.identity) this.webSocket = this.createWebSocket();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
import { persisted } from 'svelte-persisted-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 { ChatListManager } from './chatList';
|
||||
import { browser } from '$app/environment';
|
||||
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();
|
||||
searchManager: GlobalSearchManager = new GlobalSearchManager(this.connections);
|
||||
|
||||
constructor() {
|
||||
if (browser) {
|
||||
chatServers.subscribe(this.onChatServersChange.bind(this));
|
||||
// currentKeyPair.subscribe(this.onKeyPairChange.bind(this));
|
||||
}
|
||||
constructor(identity: BlahIdentity, chatServers: string[] = defaultChatServers) {
|
||||
this.#identity = identity;
|
||||
this.#chatServers = chatServers;
|
||||
}
|
||||
|
||||
private createAndConnect(endpoint: string) {
|
||||
const connection = new BlahChatServerConnection(endpoint, this.keypair);
|
||||
this.connections.set(endpoint, connection);
|
||||
const connection = new BlahChatServerConnection(endpoint, this.#identity);
|
||||
this.#connections.set(endpoint, connection);
|
||||
connection.connect();
|
||||
this.setupChatList(connection);
|
||||
}
|
||||
|
@ -84,5 +84,3 @@ class ChatServerConnectionPool {
|
|||
return this.connections.get(endpoint) ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
export const chatServerConnectionPool = new ChatServerConnectionPool();
|
||||
|
|
Loading…
Add table
Reference in a new issue