Add restoreProcesses() and restoreMessages()
This commit is contained in:
parent
c8fbf6d18a
commit
2f2b2a3a75
@ -12,6 +12,10 @@ import { servicesVersion } from 'typescript';
|
|||||||
import { ApiReturn, CachedMessage, Member } from '../../dist/pkg/sdk_client';
|
import { ApiReturn, CachedMessage, Member } from '../../dist/pkg/sdk_client';
|
||||||
import Routing from './routing.service';
|
import Routing from './routing.service';
|
||||||
|
|
||||||
|
type ProcessesCache = {
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
|
||||||
export default class Services {
|
export default class Services {
|
||||||
private static instance: Services;
|
private static instance: Services;
|
||||||
private current_process: string | null = null;
|
private current_process: string | null = null;
|
||||||
@ -415,6 +419,99 @@ export default class Services {
|
|||||||
console.log("🚀 ~ Services ~ restoreDevice ~ res:", res)
|
console.log("🚀 ~ Services ~ restoreDevice ~ res:", res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getProcessesCache(): ProcessesCache {
|
||||||
|
// Regular expression to match 64-character hexadecimal strings
|
||||||
|
const hexU32KeyRegex: RegExp = /^[0-9a-fA-F]{64}:\d+$/;
|
||||||
|
const hexObjects: ProcessesCache = {}
|
||||||
|
|
||||||
|
// Iterate over all keys in localStorage
|
||||||
|
for (let i = 0; i < localStorage.length; i++) {
|
||||||
|
const key = localStorage.key(i);
|
||||||
|
|
||||||
|
if (!key) {
|
||||||
|
return hexObjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the key matches the 32-byte hex pattern
|
||||||
|
if (hexU32KeyRegex.test(key)) {
|
||||||
|
const value = localStorage.getItem(key);
|
||||||
|
if (!value) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
hexObjects[key] = JSON.parse(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hexObjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getCachedMessages(): string[] {
|
||||||
|
const u32KeyRegex = /^\d+$/;
|
||||||
|
const U32_MAX = 4294967295;
|
||||||
|
const messages: string[] = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < localStorage.length; i++) {
|
||||||
|
const key = localStorage.key(i);
|
||||||
|
|
||||||
|
if (!key) {
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u32KeyRegex.test(key)) {
|
||||||
|
const num = parseInt(key, 10);
|
||||||
|
if (num < 0 || num > U32_MAX) {
|
||||||
|
console.warn(`Key ${key} is outside the u32 range and will be ignored.`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = localStorage.getItem(key);
|
||||||
|
if (!value) {
|
||||||
|
console.warn(`No value found for key: ${key}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
messages.push(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async restoreProcesses() {
|
||||||
|
const processesCache = this.getProcessesCache();
|
||||||
|
console.log("🚀 ~ Services ~ restoreProcesses ~ processesCache:", processesCache);
|
||||||
|
|
||||||
|
if (processesCache.length == 0) {
|
||||||
|
console.debug("🚀 ~ Services ~ restoreProcesses ~ no processes in local storage");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const services = await Services.getInstance();
|
||||||
|
try {
|
||||||
|
await services.sdkClient.set_process_cache(JSON.stringify(processesCache));
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Services ~ restoreProcesses ~ Error:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async restoreMessages() {
|
||||||
|
const cachedMessages = this.getCachedMessages();
|
||||||
|
console.log("🚀 ~ Services ~ restoreMessages ~ chachedMessages:", cachedMessages);
|
||||||
|
|
||||||
|
if (cachedMessages.length == 0) {
|
||||||
|
console.debug("🚀 ~ Services ~ restoreMessages ~ no messages in local storage");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const services = await Services.getInstance();
|
||||||
|
try {
|
||||||
|
await services.sdkClient.set_message_cache(JSON.stringify(cachedMessages));
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Services ~ restoreMessages ~ Error:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private cleanSubsciptions(): void {
|
private cleanSubsciptions(): void {
|
||||||
for (const sub of this.subscriptions) {
|
for (const sub of this.subscriptions) {
|
||||||
const el = sub.element;
|
const el = sub.element;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user