Skip to content

Commit

Permalink
fix: auto-update reload action (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio authored Apr 26, 2024
1 parent fae97f4 commit 4b7d4bd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-olives-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

fix: auto-update in background to wait for update to be downloaded
48 changes: 41 additions & 7 deletions packages/app/src/systems/CRX/background/actions/autoUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,62 @@ async function isOpen() {
return isOpen;
}

async function runVersionCheck() {
async function getLatestVersion() {
const latestVersion = await fetch(VITE_CRX_VERSION_API)
.then((res) => res.json())
// If fails to fetch the version return a empty object
.catch(() => ({}));
const version = latestVersion[WALLET_NAME] || APP_VERSION;
return latestVersion[WALLET_NAME] || APP_VERSION;
}

async function runVersionCheck() {
const version = await getLatestVersion();
// If app version is greater than the one on the release API ignores the check
if (compareVersions(APP_VERSION, version) > -1) return;
if (await isOpen()) return;
// Request update check and reload if available
console.log('[FUEL WALLET] Checking for updates...');
console.debug('[FUEL WALLET] Checking for updates...');
chrome.runtime.requestUpdateCheck((details) => {
if (details === 'update_available') {
console.log('[FUEL WALLET] Update available reload application...');
chrome.runtime.reload();
// Remove the alarm to check for updates until next reload
chrome.alarms.clear('autoUpdate');
return;
}
console.debug('[FUEL WALLET] No update available', details);
});
}

chrome.alarms.create('autoUpdate', { periodInMinutes: 5 });
async function reloadWallet() {
if (await isOpen()) {
console.debug('[FUEL WALLET] Wallet is open, waiting 5 minutes...');
// If the wallet is open, wait 5 minutes and try again
chrome.alarms.create('reloadWallet', { delayInMinutes: 5 });
return;
}
// Check if reload already happened
const version = await getLatestVersion();
if (APP_VERSION === version) {
chrome.alarms.clear('reloadWallet');
return;
}
chrome.runtime.reload();
}

// Once the app is updated reload the wallet
chrome.runtime.onUpdateAvailable.addListener(() => reloadWallet());
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === 'autoUpdate') {
runVersionCheck();
switch (alarm.name) {
case 'autoUpdate':
runVersionCheck();
break;
case 'reloadWallet':
reloadWallet();
break;
default:
break;
}
});

// Register alarms to check for updates and reload the wallet
chrome.alarms.create('autoUpdate', { periodInMinutes: 10 });

0 comments on commit 4b7d4bd

Please sign in to comment.