From dc698724acf49fdd99c6d494c04400f12f31696f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 10 May 2024 11:06:42 +0200 Subject: [PATCH] MOBILE-4470 sqlite: Reload page when OPFS gets disabled (workaround) --- .../emulator/classes/wasm-sqlite-object.ts | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/core/features/emulator/classes/wasm-sqlite-object.ts b/src/core/features/emulator/classes/wasm-sqlite-object.ts index 8dc35cddd..9a9a8fffd 100644 --- a/src/core/features/emulator/classes/wasm-sqlite-object.ts +++ b/src/core/features/emulator/classes/wasm-sqlite-object.ts @@ -17,6 +17,7 @@ import { SQLiteObject } from '@awesome-cordova-plugins/sqlite/ngx'; import { CorePromisedValue } from '@classes/promised-value'; +import { CoreLogger } from '@singletons/logger'; import { Sqlite3Worker1Promiser, sqlite3Worker1Promiser } from '@sqlite.org/sqlite-wasm'; /** @@ -36,10 +37,13 @@ export class WasmSQLiteObject implements SQLiteObject { private name: string; private promisedPromiser: CorePromisedValue; private promiser: Sqlite3Worker1Promiser; + protected logger: CoreLogger; constructor(name: string) { this.name = name; this.promisedPromiser = new CorePromisedValue(); + this.logger = CoreLogger.getInstance('WasmSQLiteObject'); + this.promiser = async (...args) => { const promiser = await this.promisedPromiser; @@ -50,7 +54,7 @@ export class WasmSQLiteObject implements SQLiteObject { /** * Delete the database. */ - async delete(): Promise { + async delete(): Promise { if (!this.promisedPromiser.isResolved()) { await this.open(); } @@ -61,20 +65,35 @@ export class WasmSQLiteObject implements SQLiteObject { /** * @inheritdoc */ - async open(): Promise { + async open(): Promise { const promiser = await new Promise((resolve) => { const _promiser = sqlite3Worker1Promiser(() => resolve(_promiser)); }); - await promiser('open', { filename: `file:${this.name}.sqlite3`, vfs: 'opfs' }); + const response = await promiser('config-get', {}); + const isEnabled = (response as any).result.opfsEnabled; + if (!isEnabled) { + this.logger.error('opfsEnabled flag is disabled. Reloading the page.'); + // @TODO Fix Workaround for the issue with the opfsEnabled flag. + // The flag gets disabled when opening a database, so we need to reload the page to make it work. + window.location.reload(); + } + + try { + await promiser('open', { filename: `file:${this.name}.sqlite3`, vfs: 'opfs' }); + } catch (error) { + this.logger.error(`Error opening database: ${this.name}. Details: ${error.result.message}`); + + throw error; + } this.promisedPromiser.resolve(promiser); } /** * @inheritdoc */ - async close(): Promise { + async close(): Promise { await this.promiser('close', {}); }