Skip to content

Commit a796df4

Browse files
authored
feat: restore DB connection on close (#1276)
Implements same fix as #1270 but without an interval.
1 parent 706aeb3 commit a796df4

File tree

2 files changed

+12
-38
lines changed

2 files changed

+12
-38
lines changed

.changeset/friendly-ears-sell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fuels-wallet": patch
3+
---
4+
5+
restore DB connection on close, removed interval

packages/app/src/systems/Core/utils/database.ts

+7-38
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,11 @@ export class FuelDB extends Dexie {
5757
});
5858
this.on('blocked', () => this.restart('blocked'));
5959
this.on('close', () => this.restart('close'));
60-
this.on('message', (e) => {
61-
console.log('fsk changed', e);
62-
});
6360
}
6461

65-
open() {
66-
return this.safeOpen().finally(() =>
67-
this.watchConnection()
68-
) as PromiseExtended<Dexie>;
69-
}
70-
71-
close(safeClose = false) {
72-
if (safeClose) {
73-
this.restartAttempts = 0;
74-
clearInterval(this.integrityCheckInterval);
75-
}
76-
return super.close();
77-
}
78-
79-
async safeOpen() {
62+
open(): PromiseExtended<Dexie> {
8063
try {
81-
const result = await super.open();
64+
const result = super.open();
8265
this.restartAttempts = 0;
8366
return result;
8467
} catch (err) {
@@ -88,27 +71,13 @@ export class FuelDB extends Dexie {
8871
}
8972
}
9073

91-
async ensureDatabaseOpen() {
92-
if (this.isOpen() && !this.hasBeenClosed() && !this.hasFailed()) return;
93-
94-
if (this.restartAttempts > 3) {
95-
console.error('Reached max attempts to open DB. Sending restart signal.');
96-
this.restart('blocked');
97-
return;
74+
async close(safeClose = false) {
75+
if (!this.alwaysOpen || safeClose || this.restartAttempts > 3) {
76+
this.restartAttempts = 0;
77+
return super.close();
9878
}
99-
10079
this.restartAttempts += 1;
101-
console.warn('DB is not open. Attempting restart.');
102-
await this.safeOpen();
103-
}
104-
105-
watchConnection() {
106-
if (!this.alwaysOpen) return;
107-
108-
clearInterval(this.integrityCheckInterval);
109-
this.integrityCheckInterval = setInterval(() => {
110-
this.ensureDatabaseOpen();
111-
}, 1000);
80+
await this.open().catch(() => this.close());
11281
}
11382

11483
async restart(eventName: FailureEvents) {

0 commit comments

Comments
 (0)