Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failed dApp connector API calls after service worker going to sleep #3728

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export async function raii<T>(
)
).join('\n');
// eslint-disable-next-line no-console
console.error('rolling back Lovefield query for\n' + uniqueTableNames + ' with error ' + JSON.stringify(e));
console.error('rolling back Lovefield query for\n' + uniqueTableNames + ' with error ' + e.message);
await tx.rollback();
throw e;
}
Expand Down
22 changes: 20 additions & 2 deletions packages/yoroi-extension/chrome/content-scripts/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ function listenToBackgroundServiceWorker() {
connected = true;
}

const RETRY_COUNT = 1;
const RETRY_DELAY = 3000;

async function sendMessageToBackground(message) {
for (let c = 0; c <= RETRY_COUNT; c++) {
try {
return chrome.runtime.sendMessage(message);
} catch (error) {
if (error.message.includes('Could not establish connection. Receiving end does not exist.')) {
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
continue;
} else {
throw error;
}
}
}
}

async function handleConnectorConnectRequest(event, protocol) {
const requestIdentification = event.data.requestIdentification;
if ((cardanoApiInjected && !requestIdentification) && connected) {
Expand Down Expand Up @@ -158,7 +176,7 @@ async function handleConnectorConnectRequest(event, protocol) {
},
protocol,
};
chrome.runtime.sendMessage(message);
sendMessageToBackground(message);
});
}
}
Expand All @@ -169,7 +187,7 @@ async function handleConnectorRpcRequest(event) {
listenToBackgroundServiceWorker();
}
try {
await chrome.runtime.sendMessage(event.data);
await sendMessageToBackground(event.data);
} catch (e) {
console.error(`Could not send RPC to Yoroi: ${e}`);
window.postMessage({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ import { migrateNoRefresh } from '../../../../app/api/common/migration';
import LocalStorageApi from '../../../../app/api/localStorage/index';
import type { lf$Database, } from 'lovefield';

let dbCache = null;
let loadDbPromiseCache = null;
let migratePromiseCache = null;

export async function getDb(): Promise<lf$Database> {
const localStorageApi = new LocalStorageApi();

if (!dbCache) {
dbCache = await loadLovefieldDB(schema.DataStoreType.INDEXED_DB);
if (!loadDbPromiseCache) {
loadDbPromiseCache = loadLovefieldDB(schema.DataStoreType.INDEXED_DB);
}

const db = await loadDbPromiseCache;

if (!migratePromiseCache) {
migratePromiseCache = migrateNoRefresh({
localStorageApi,
persistentDb: dbCache,
persistentDb: db,
currVersion: environment.getVersion(),
});
}
await migratePromiseCache;

return dbCache;
return db;
}
Loading