diff --git a/src/pages/chat/chat.ts b/src/pages/chat/chat.ts index af63e64..71ed8ec 100755 --- a/src/pages/chat/chat.ts +++ b/src/pages/chat/chat.ts @@ -123,6 +123,23 @@ class ChatElement extends HTMLElement { } }; + document.addEventListener('newDataReceived', async (event: CustomEvent) => { + const { detail } = event; + console.log('New data event received:', JSON.stringify(detail)); + + if (detail.processId && detail.processId === this.processId) { + console.log('Detected update to chat'); + if (this.selectedMember) { + await this.loadMemberChat(this.selectedMember); + } else { + console.error('No selected member?'); + } + } else { + console.log('Received an update for another process'); + } + }); + + document.addEventListener('DOMContentLoaded', () => { this.notificationBadge = document.querySelector('.notification-badge'); this.notificationBoard = document.getElementById('notification-board'); diff --git a/src/services/database.service.ts b/src/services/database.service.ts index a783b9a..9a4dda1 100755 --- a/src/services/database.service.ts +++ b/src/services/database.service.ts @@ -190,7 +190,6 @@ export class Database { private async handleServiceWorkerMessage(message: any) { switch (message.type) { case 'TO_DOWNLOAD': - console.log('Received data to download:', message.data); await this.handleDownloadList(message.data); break; default: @@ -203,19 +202,31 @@ export class Database { let requestedStateId = []; const service = await Services.getInstance(); for (const hash of downloadList) { + const diff = await service.getDiffByValue(hash); + if (!diff) { + // This should never happen + console.warn(`Missing a diff for hash ${hash}`); + continue; + } + const processId = diff.process_id; + const stateId = diff.state_id; try { const valueBytes = await service.fetchValueFromStorage(hash); if (valueBytes) { // Save data to db const blob = new Blob([valueBytes], {type: "application/octet-stream"}); await service.saveBlobToDb(hash, blob); + document.dispatchEvent(new CustomEvent('newDataReceived', { + detail: { + processId, + stateId, + hash, + } + })); } else { // We first request the data from managers console.log('Request data from managers of the process'); // get the diff from db - const diff = await service.getDiffByValue(hash); - const processId = diff.process_id; - const stateId = diff.state_id; if (!requestedStateId.includes(stateId)) { await service.requestDataFromPeers(processId, stateId); requestedStateId.push(stateId);