Chat updates when received message

This commit is contained in:
NicolasCantu 2025-02-27 12:34:56 +01:00
parent ed578be468
commit 7c3e263b8a
2 changed files with 32 additions and 4 deletions

View File

@ -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', () => { document.addEventListener('DOMContentLoaded', () => {
this.notificationBadge = document.querySelector('.notification-badge'); this.notificationBadge = document.querySelector('.notification-badge');
this.notificationBoard = document.getElementById('notification-board'); this.notificationBoard = document.getElementById('notification-board');

View File

@ -190,7 +190,6 @@ export class Database {
private async handleServiceWorkerMessage(message: any) { private async handleServiceWorkerMessage(message: any) {
switch (message.type) { switch (message.type) {
case 'TO_DOWNLOAD': case 'TO_DOWNLOAD':
console.log('Received data to download:', message.data);
await this.handleDownloadList(message.data); await this.handleDownloadList(message.data);
break; break;
default: default:
@ -203,19 +202,31 @@ export class Database {
let requestedStateId = []; let requestedStateId = [];
const service = await Services.getInstance(); const service = await Services.getInstance();
for (const hash of downloadList) { 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 { try {
const valueBytes = await service.fetchValueFromStorage(hash); const valueBytes = await service.fetchValueFromStorage(hash);
if (valueBytes) { if (valueBytes) {
// Save data to db // Save data to db
const blob = new Blob([valueBytes], {type: "application/octet-stream"}); const blob = new Blob([valueBytes], {type: "application/octet-stream"});
await service.saveBlobToDb(hash, blob); await service.saveBlobToDb(hash, blob);
document.dispatchEvent(new CustomEvent('newDataReceived', {
detail: {
processId,
stateId,
hash,
}
}));
} else { } else {
// We first request the data from managers // We first request the data from managers
console.log('Request data from managers of the process'); console.log('Request data from managers of the process');
// get the diff from db // 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)) { if (!requestedStateId.includes(stateId)) {
await service.requestDataFromPeers(processId, stateId); await service.requestDataFromPeers(processId, stateId);
requestedStateId.push(stateId); requestedStateId.push(stateId);