Fix bug where Services instance seemed to get dropped

This commit is contained in:
Sosthene 2024-10-12 15:09:03 +02:00
parent 9f45b2c788
commit 8d745693b7
2 changed files with 38 additions and 33 deletions

View File

@ -1,8 +1,5 @@
import Services from './services/service'; import Services from './services/service';
// import { WebSocketClient } from './websockets';
// const wsurl = `ws://74.234.68.175:8091`;
const wsurl = `wss://localhost/ws/`;
document.addEventListener('DOMContentLoaded', async () => { document.addEventListener('DOMContentLoaded', async () => {
try { try {
@ -14,7 +11,6 @@ try {
} else { } else {
await services.restoreDevice(device) await services.restoreDevice(device)
} }
await services.addWebsocketConnection(wsurl);
await services.recoverInjectHtml(); await services.recoverInjectHtml();
await services.restoreProcesses(); await services.restoreProcesses();
await services.restoreMessages(); await services.restoreMessages();

View File

@ -17,8 +17,10 @@ type ProcessesCache = {
}; };
const U32_MAX = 4294967295; const U32_MAX = 4294967295;
const wsurl = `ws://localhost:8090`;
export default class Services { export default class Services {
private static initializing: Promise<Services> | null = null;
private static instance: Services; private static instance: Services;
private current_process: string | null = null; private current_process: string | null = null;
private sdkClient: any; private sdkClient: any;
@ -32,25 +34,35 @@ export default class Services {
// Method to access the singleton instance of Services // Method to access the singleton instance of Services
public static async getInstance(): Promise<Services> { public static async getInstance(): Promise<Services> {
if (!Services.instance) { if (Services.instance) {
Services.instance = new Services(); return Services.instance;
await Services.instance.init();
} }
if (!Services.initializing) {
Services.initializing = (async () => {
const instance = new Services();
await instance.init();
return instance;
})();
}
Services.instance = await Services.initializing;
Services.initializing = null; // Reset for potential future use
return Services.instance; return Services.instance;
} }
public async init(): Promise<void> { public async init(): Promise<void> {
this.notifications = this.getNotifications(); this.notifications = this.getNotifications();
this.sdkClient = await import("../../dist/pkg/sdk_client"); this.sdkClient = await import("../../dist/pkg/sdk_client");
// this.database = Database.getInstance() await this.addWebsocketConnection(wsurl);
} }
public async addWebsocketConnection(url: string): Promise<void> { public async addWebsocketConnection(url: string): Promise<void> {
const services = await Services.getInstance(); // const services = await Services.getInstance();
const newClient = new WebSocketClient(url, services); if (!this.websocketConnection) {
if (!services.websocketConnection) { console.log('Opening new websocket connection');
services.websocketConnection = newClient; const newClient = new WebSocketClient(url, this);
this.websocketConnection = newClient;
} }
} }
@ -73,9 +85,6 @@ export default class Services {
if(btn) { if(btn) {
this.addSubscription(btn, 'click', 'injectProcessListPage') this.addSubscription(btn, 'click', 'injectProcessListPage')
} }
// const url = location.href
const services = await Services.getInstance();
const localAddress = await this.getDeviceAddress(); const localAddress = await this.getDeviceAddress();
this.generateQRCode(localAddress || '') this.generateQRCode(localAddress || '')