Chat updates when received message
This commit is contained in:
parent
ed578be468
commit
7c3e263b8a
@ -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');
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user