wip: optimizations
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
} from '../../shared-kernel';
|
||||
import type { ChatAttachmentMeta } from '../../shared-kernel';
|
||||
import { getStoredCurrentUserId } from '../../core/storage/current-user-storage';
|
||||
import type { RoomMessageStats } from './database.service';
|
||||
|
||||
/** IndexedDB database name for the MetoYou application. */
|
||||
const DATABASE_NAME = 'metoyou';
|
||||
@@ -110,6 +111,14 @@ export class BrowserDatabaseService {
|
||||
return this.hydrateMessages(messages);
|
||||
}
|
||||
|
||||
async getRoomMessageStats(roomId: string): Promise<RoomMessageStats> {
|
||||
return this.foldMessagesForRoom(roomId, (stats, message) => {
|
||||
stats.count += 1;
|
||||
stats.lastUpdated = Math.max(stats.lastUpdated, message.editedAt || message.timestamp || 0);
|
||||
}, { count: 0,
|
||||
lastUpdated: 0 });
|
||||
}
|
||||
|
||||
/** Delete a message by its ID. */
|
||||
async deleteMessage(messageId: string): Promise<void> {
|
||||
await this.deleteRecord(STORE_MESSAGES, messageId);
|
||||
@@ -533,6 +542,36 @@ export class BrowserDatabaseService {
|
||||
});
|
||||
}
|
||||
|
||||
private async foldMessagesForRoom<T>(
|
||||
roomId: string,
|
||||
visit: (state: T, message: Message) => void,
|
||||
initialState: T
|
||||
): Promise<T> {
|
||||
const transaction = this.createTransaction(STORE_MESSAGES, 'readonly');
|
||||
const request = transaction.objectStore(STORE_MESSAGES)
|
||||
.index('roomId')
|
||||
.openCursor(IDBKeyRange.only(roomId));
|
||||
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const state = initialState;
|
||||
|
||||
request.onsuccess = () => {
|
||||
const cursor = request.result;
|
||||
|
||||
if (!cursor) {
|
||||
resolve(state);
|
||||
return;
|
||||
}
|
||||
|
||||
visit(state, cursor.value as Message);
|
||||
cursor.continue();
|
||||
};
|
||||
|
||||
request.onerror = () => reject(request.error);
|
||||
transaction.onabort = () => reject(transaction.error);
|
||||
});
|
||||
}
|
||||
|
||||
private async deleteRecord(storeName: string, key: IDBValidKey): Promise<void> {
|
||||
const transaction = this.createTransaction(storeName, 'readwrite');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user