Fix Process storage
This commit is contained in:
parent
8810090370
commit
78e868d1f6
@ -36,7 +36,7 @@ export async function init() {
|
|||||||
|
|
||||||
const processes = await getProcesses();
|
const processes = await getProcesses();
|
||||||
for (let process of processes) {
|
for (let process of processes) {
|
||||||
const processName = process[1].title;
|
const processName = process['description'];
|
||||||
const opt = new Option(processName);
|
const opt = new Option(processName);
|
||||||
opt.value = processName;
|
opt.value = processName;
|
||||||
element.add(opt);
|
element.add(opt);
|
||||||
@ -364,7 +364,7 @@ async function showSelectedProcess(elem: MouseEvent) {
|
|||||||
titleDiv.className = 'process-title';
|
titleDiv.className = 'process-title';
|
||||||
titleDiv.innerHTML = `${process[1].title} : ${process[1].description}`;
|
titleDiv.innerHTML = `${process[1].title} : ${process[1].description}`;
|
||||||
processDiv.appendChild(titleDiv);
|
processDiv.appendChild(titleDiv);
|
||||||
for (const zone of process[1].zones) {
|
for (const zone of process.zones) {
|
||||||
const zoneElement = document.createElement('div');
|
const zoneElement = document.createElement('div');
|
||||||
zoneElement.className = 'process-element';
|
zoneElement.className = 'process-element';
|
||||||
const zoneId = process[1].title + '-' + zone.id;
|
const zoneId = process[1].title + '-' + zone.id;
|
||||||
@ -408,5 +408,10 @@ async function getProcesses(): Promise<any[]> {
|
|||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
const processes = await service.getProcesses();
|
const processes = await service.getProcesses();
|
||||||
|
|
||||||
return processes;
|
const res = Object.entries(processes).map(([key, value]) => ({
|
||||||
|
key,
|
||||||
|
value
|
||||||
|
}))
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@ class Database {
|
|||||||
indices: [],
|
indices: [],
|
||||||
},
|
},
|
||||||
AnkProcess: {
|
AnkProcess: {
|
||||||
name: 'process',
|
name: 'processes',
|
||||||
options: { keyPath: 'id' },
|
options: {},
|
||||||
indices: [],
|
indices: [],
|
||||||
},
|
},
|
||||||
AnkSharedSecrets: {
|
AnkSharedSecrets: {
|
||||||
@ -27,11 +27,6 @@ class Database {
|
|||||||
options: { autoIncrement: true },
|
options: { autoIncrement: true },
|
||||||
indices: [],
|
indices: [],
|
||||||
},
|
},
|
||||||
AnkProcessData: {
|
|
||||||
name: 'process-data',
|
|
||||||
options: { keyPath: 'id' },
|
|
||||||
indices: [],
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Private constructor to prevent direct instantiation from outside
|
// Private constructor to prevent direct instantiation from outside
|
||||||
@ -191,6 +186,36 @@ class Database {
|
|||||||
});
|
});
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async dumpStore(storeName: string): Promise<Record<string, any>> {
|
||||||
|
const db = await this.getDb();
|
||||||
|
const tx = db.transaction(storeName, 'readonly');
|
||||||
|
const store = tx.objectStore(storeName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Wait for both getAllKeys() and getAll() to resolve
|
||||||
|
const [keys, values] = await Promise.all([
|
||||||
|
new Promise<any[]>((resolve, reject) => {
|
||||||
|
const request = store.getAllKeys();
|
||||||
|
request.onsuccess = () => resolve(request.result);
|
||||||
|
request.onerror = () => reject(request.error);
|
||||||
|
}),
|
||||||
|
new Promise<any[]>((resolve, reject) => {
|
||||||
|
const request = store.getAll();
|
||||||
|
request.onsuccess = () => resolve(request.result);
|
||||||
|
request.onerror = () => reject(request.error);
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Combine keys and values into an object
|
||||||
|
const result: Record<string, any> = Object.fromEntries(keys.map((key, index) => [key, values[index]]));
|
||||||
|
return result;
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching data from IndexedDB:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Database;
|
export default Database;
|
||||||
|
@ -3,7 +3,7 @@ import { INotification } from '~/models/notification.model';
|
|||||||
import { IProcess } from '~/models/process.model';
|
import { IProcess } from '~/models/process.model';
|
||||||
// import Database from './database';
|
// import Database from './database';
|
||||||
import { initWebsocket, sendMessage } from '../websockets';
|
import { initWebsocket, sendMessage } from '../websockets';
|
||||||
import { ApiReturn, Member } from '../../dist/pkg/sdk_client';
|
import { ApiReturn, Member, Process } from '../../dist/pkg/sdk_client';
|
||||||
import ModalService from './modal.service';
|
import ModalService from './modal.service';
|
||||||
import { navigate } from '../router';
|
import { navigate } from '../router';
|
||||||
import Database from './database.service';
|
import Database from './database.service';
|
||||||
@ -305,16 +305,18 @@ export default class Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
if (apiReturn.updated_process && apiReturn.updated_process.length) {
|
if (apiReturn.updated_process) {
|
||||||
const [commitmentTx, process] = apiReturn.updated_process;
|
const updatedProcess = apiReturn.updated_process;
|
||||||
|
|
||||||
// Save process to storage
|
// Save process to storage
|
||||||
const db = await Database.getInstance();
|
try {
|
||||||
db.addObject({
|
await this.saveProcess(updatedProcess.commitment_tx, updatedProcess.current_process);
|
||||||
storeName: 'process',
|
} catch (e) {
|
||||||
object: { id: commitmentTx, process },
|
throw e;
|
||||||
key: null,
|
}
|
||||||
});
|
|
||||||
|
// do we need to act?
|
||||||
|
updatedProcess.user_validation_required
|
||||||
}
|
}
|
||||||
|
|
||||||
if (apiReturn.commit_to_send) {
|
if (apiReturn.commit_to_send) {
|
||||||
@ -444,86 +446,46 @@ export default class Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getProcesses(): Promise<any[]> {
|
public async saveProcess(commitedIn: string, process: any) {
|
||||||
const process = [
|
const db = await Database.getInstance();
|
||||||
[
|
try {
|
||||||
'1',
|
await db.addObject({
|
||||||
{
|
storeName: 'processes',
|
||||||
title: 'Messaging',
|
object: process,
|
||||||
description: 'Messagerie chiffrée',
|
key: commitedIn,
|
||||||
html: '<div><input /></div>',
|
});
|
||||||
css: '',
|
} catch (e) {
|
||||||
script: '',
|
throw new Error(`Failed to save process: ${e}`)
|
||||||
zones: [
|
}
|
||||||
{
|
|
||||||
id: '1',
|
|
||||||
title: 'zone 1',
|
|
||||||
description: 'zone 1',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '2',
|
|
||||||
title: 'zone 2',
|
|
||||||
description: 'zone 2',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
roles: {
|
|
||||||
owner: {
|
|
||||||
members: ['dfdsfdfdsf', 'dfdfdfdsfsfdfdsf'],
|
|
||||||
validation_rules: [
|
|
||||||
{
|
|
||||||
quorum: 1.0,
|
|
||||||
fields: ['description', 'roles', 'session_privkey', 'session_pubkey', 'key_parity'],
|
|
||||||
min_sig_member: 1.0,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'2',
|
|
||||||
{
|
|
||||||
title: 'Database',
|
|
||||||
description: 'Database chiffrée',
|
|
||||||
html: '<div><input value="{{inputValue}}" /></div>',
|
|
||||||
css: '',
|
|
||||||
script: '',
|
|
||||||
zones: [
|
|
||||||
{
|
|
||||||
id: '1',
|
|
||||||
title: 'zone 1',
|
|
||||||
description: 'zone 1',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '2',
|
|
||||||
title: 'zone 2',
|
|
||||||
description: 'zone 2',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
roles: {
|
|
||||||
owner: {
|
|
||||||
members: ['dfdsfdfdsf', 'dfdfdfdsfsfdfdsf'],
|
|
||||||
validation_rules: [
|
|
||||||
{
|
|
||||||
quorum: 1.0,
|
|
||||||
fields: ['description', 'roles', 'session_privkey', 'session_pubkey', 'key_parity'],
|
|
||||||
min_sig_member: 1.0,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
];
|
|
||||||
return process;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private getProcessesCache() {
|
public async getProcess(commitedIn: string): Promise<Process> {
|
||||||
console.log('TODO get processes from indexedDB')
|
const db = await Database.getInstance();
|
||||||
|
return await db.getObject('processes', commitedIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getProcesses(): Promise<Record<string, Process>> {
|
||||||
|
const db = await Database.getInstance();
|
||||||
|
|
||||||
|
const processes: Record<string, Process> = await db.dumpStore('processes');
|
||||||
|
return processes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore process in wasm with persistent storage
|
||||||
public async restoreProcesses() {
|
public async restoreProcesses() {
|
||||||
console.log('TODO: restore processes from indexedDB');
|
const db = await Database.getInstance();
|
||||||
|
try {
|
||||||
|
const processes: Record<string, Process> = await db.dumpStore('processes');
|
||||||
|
if (processes && Object.keys(processes).length != 0) {
|
||||||
|
console.log(`Restoring ${Object.keys(processes).length} processes`);
|
||||||
|
this.sdkClient.set_process_cache(JSON.stringify(processes));
|
||||||
|
} else {
|
||||||
|
console.log('No processes to restore!');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getNotifications(): INotification[] {
|
getNotifications(): INotification[] {
|
||||||
|
@ -166,14 +166,15 @@ async function onOkButtonClick() {
|
|||||||
if (!createPairingProcessReturn.updated_process) {
|
if (!createPairingProcessReturn.updated_process) {
|
||||||
throw new Error('createPairingProcess returned an empty new process'); // This should never happen
|
throw new Error('createPairingProcess returned an empty new process'); // This should never happen
|
||||||
}
|
}
|
||||||
const [commitmentOutpoint, process] = createPairingProcessReturn.updated_process;
|
|
||||||
|
const commitmentOutpoint = createPairingProcessReturn.updated_process.commitment_tx;
|
||||||
|
|
||||||
// We set the service to the process
|
// We set the service to the process
|
||||||
service.getUpdateProposals(commitmentOutpoint);
|
service.getUpdateProposals(commitmentOutpoint);
|
||||||
|
|
||||||
await service.evaluatePendingUpdates();
|
await service.evaluatePendingUpdates();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('onOkButtonClick error:', e);
|
console.error(`onOkButtonClick error: ${e}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user