Skip to content

Commit a43e6c2

Browse files
authored
Merge pull request #1029 from getAlby/fix/refactor-db-handling
fix: refactor DB loading of allowances/payments
2 parents 36b8822 + 269fa90 commit a43e6c2

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

src/extension/background-script/db.ts

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,49 @@ class DB extends Dexie {
5959
}
6060

6161
async loadFromStorage() {
62-
try {
63-
const result = await browser.storage.local.get([
64-
"allowances",
65-
"payments",
66-
]);
67-
console.log("Loading DB data from storage");
68-
if (result.allowances) {
69-
await this.allowances.bulkAdd(result.allowances);
70-
}
71-
if (result.payments) {
72-
await this.payments.bulkAdd(result.payments);
73-
}
74-
return true;
75-
} catch (e) {
76-
console.log("Failed to load DB data from storage");
77-
console.log(e);
78-
}
62+
console.log("Loading DB data from storage");
63+
return browser.storage.local
64+
.get(["allowances", "payments"])
65+
.then((result) => {
66+
const allowancePromise = this.allowances.count().then((count) => {
67+
// if the DB already has entries we do not need to add the data from the browser storage. We then already have the data in the indexeddb
68+
if (count > 0) {
69+
console.log(`Found ${count} allowances already in the DB`);
70+
return;
71+
} else if (result.allowances && result.allowances.length > 0) {
72+
// adding the data from the browser storage
73+
return this.allowances
74+
.bulkAdd(result.allowances)
75+
.catch(Dexie.BulkError, function (e) {
76+
console.log("Failed to add allowances; ignoring");
77+
console.error(e);
78+
});
79+
}
80+
});
81+
82+
const paymentsPromise = this.payments.count().then((count) => {
83+
// if the DB already has entries we do not need to add the data from the browser storage. We then already have the data in the indexeddb
84+
if (count > 0) {
85+
console.log(`Found ${count} payments already in the DB`);
86+
return;
87+
} else if (result.payments && result.payments.length > 0) {
88+
// adding the data from the browser storage
89+
return this.payments
90+
.bulkAdd(result.payments)
91+
.catch(Dexie.BulkError, function (e) {
92+
console.log("Failed to add payments; ignoring");
93+
console.error(e);
94+
});
95+
}
96+
});
97+
98+
// wait for all allowances and payments to be loaded
99+
return Promise.all([allowancePromise, paymentsPromise]);
100+
})
101+
.catch((e) => {
102+
console.log("Failed to load DB data from storage");
103+
console.error(e);
104+
});
79105
}
80106
}
81107

src/extension/background-script/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,17 @@ async function init() {
107107
//await browser.storage.sync.set({ settings: { debug: true }, allowances: [] });
108108
await state.getState().init();
109109
console.log("State loaded");
110+
110111
await db.open();
112+
console.log("DB opened");
111113

112114
events.subscribe();
115+
console.log("Events subscribed");
113116

114-
// initialize a connector for the current account
115117
browser.runtime.onMessage.addListener(debugLogger);
116118
// this is the only handler that may and must return a Promise which resolve with the response to the content script
117119
browser.runtime.onMessage.addListener(routeCalls);
118120

119-
// TODO: make optional
120121
browser.tabs.onUpdated.addListener(updateIcon); // update Icon when there is an allowance
121122

122123
// Notify the content script that the tab has been updated.
@@ -131,12 +132,14 @@ async function init() {
131132
router,
132133
};
133134
}
135+
console.log("Loading completed");
134136
}
135137

136138
// The onInstalled event is fired directly after the code is loaded.
137139
// When we subscribe to that event asynchronously in the init() function it is too late and we miss the event.
138140
browser.runtime.onInstalled.addListener(handleInstalled);
139141

142+
console.log("Welcome to Alby");
140143
init().then(() => {
141144
if (isFirstInstalled) {
142145
utils.openUrl("welcome.html");

0 commit comments

Comments
 (0)