Skip to content

Commit a9e960c

Browse files
authored
fix: keep firefox background script alive (#1885)
1 parent ce6ea11 commit a9e960c

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

apps/browser-extension-wallet/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"cleanup:firefox-artifacts": "rm -rf artifacts-firefox",
3838
"cleanup:node_modules": "rm -rf node_modules",
3939
"dev": "run-s cleanup:dist dev:project",
40-
"dev:firefox": "BROWSER=firefox run-s cleanup:dist watch:sw serve:app ",
40+
"dev:firefox": "BROWSER=firefox run-s cleanup:dist dev:project ",
4141
"dev:project": "run-p watch:sw serve:app",
4242
"format-check": "echo \"@lace/browser-extension-wallet: no format-check command specified\"",
4343
"lint": "cd ../.. && yarn extension:lint",

apps/browser-extension-wallet/src/hooks/__tests__/useMaxAda.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
import { useMaxAda, UTXO_DEPLETED_ADA_BUFFER } from '../useMaxAda';
55
import { renderHook } from '@testing-library/react-hooks';
66
import { mockWalletInfoTestnet } from '@src/utils/mocks/test-helpers';
7-
import { Subject, of } from 'rxjs';
7+
import { Subject, of, BehaviorSubject } from 'rxjs';
88
import { Wallet } from '@lace/cardano';
99
import { waitFor } from '@testing-library/react';
1010
import { act } from 'react-dom/test-utils';
11+
import { Status } from '@components/WalletStatus';
1112

1213
const MIN_COINS_FOR_TOKENS = 1_155_080;
1314
const TX_FEE = 155_381;
1415

16+
const statusSubject = new BehaviorSubject({ text: 'test', status: Status.SYNCED });
17+
1518
const inspect = jest.fn();
1619
const mockCreateTxBuilder = jest.fn().mockReturnValue({
1720
inspect,
@@ -38,7 +41,8 @@ jest.mock('../../stores', () => ({
3841
useWalletStore: () => ({
3942
walletInfo: mockWalletInfoTestnet,
4043
inMemoryWallet
41-
})
44+
}),
45+
useSyncStatus: () => statusSubject
4246
}));
4347

4448
const outputMap = new Map();

apps/browser-extension-wallet/src/hooks/useMaxAda.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/* eslint-disable no-magic-numbers */
22
import { useEffect, useState } from 'react';
33
import { Wallet } from '@lace/cardano';
4-
import { useWalletStore } from '@src/stores';
4+
import { useSyncStatus, useWalletStore } from '@src/stores';
55
import { useObservable } from '@lace/common';
66
import { OutputsMap, useMaxAdaStatus, useTransactionProps } from '@src/views/browser-view/features/send-transaction';
77
import { TxBuilder } from '@cardano-sdk/tx-construction';
88
import { Assets, WalletUtil } from '@cardano-sdk/wallet';
99
import pDebounce from 'p-debounce';
1010
import { subtractValueQuantities } from '@cardano-sdk/core';
11+
import { Status } from '@components/WalletStatus';
1112

1213
const { getTotalMinimumCoins, setMissingCoins } = Wallet;
1314

@@ -236,13 +237,15 @@ export const dCalculateMaxAda = pDebounce.promise(calculateMaxAda);
236237

237238
export const useMaxAda = (): bigint => {
238239
const [maxADA, setMaxADA] = useState<bigint>(BigInt(0));
240+
const status$ = useSyncStatus();
239241
const { walletInfo, inMemoryWallet } = useWalletStore();
240242
const balance = useObservable(inMemoryWallet?.balance?.utxo.available$);
241243
const availableRewards = useObservable(inMemoryWallet?.balance?.rewardAccounts?.rewards$);
242244
const assetInfo = useObservable(inMemoryWallet?.assetInfo$);
243245
const { outputsMap } = useTransactionProps();
244246
const { setMaxAdaLoading } = useMaxAdaStatus();
245247
const address = walletInfo?.addresses[0].address;
248+
const walletStatus = useObservable(status$);
246249

247250
useEffect(() => {
248251
const abortController = new AbortController();
@@ -289,13 +292,13 @@ export const useMaxAda = (): bigint => {
289292
}
290293
};
291294

292-
if (balance && address) {
295+
if (balance && address && walletStatus.status === Status.SYNCED) {
293296
calculate();
294297
}
295298
return () => {
296299
abortController.abort();
297300
};
298-
}, [availableRewards, assetInfo, balance, inMemoryWallet, address, outputsMap, setMaxAdaLoading]);
301+
}, [availableRewards, assetInfo, balance, inMemoryWallet, address, outputsMap, setMaxAdaLoading, walletStatus]);
299302

300303
return maxADA;
301304
};

apps/browser-extension-wallet/src/lib/scripts/background/keep-alive-sw.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ runtime.onConnect.addListener((port: TimedPort) => {
2020
port.onDisconnect.addListener(deleteTimer);
2121
port._timer = setTimeout(forceReconnect, 250e3, port);
2222
});
23+
24+
if (process.env.BROWSER === 'firefox') {
25+
runtime.onMessage.addListener(() => Promise.resolve('Pong'));
26+
}
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
import { runtime } from 'webextension-polyfill';
2+
import { logger } from '@lace/common';
23

3-
(function connect() {
4+
const TIMEOUT_DURATION = 5000;
5+
6+
const setupKeepAliveConnection = () => {
47
const port = runtime.connect({ name: 'keepAlive' });
5-
port.onDisconnect.addListener(connect);
6-
})();
8+
port.onDisconnect.addListener(setupKeepAliveConnection);
9+
};
10+
11+
const setupFirefoxWakeInterval = () => {
12+
if (process.env.BROWSER === 'firefox') {
13+
setInterval(() => {
14+
runtime
15+
.sendMessage('ping')
16+
.then((response) => {
17+
logger.debug('Response received:', response);
18+
})
19+
.catch((error) => {
20+
logger.error('Connection error:', error);
21+
});
22+
}, TIMEOUT_DURATION);
23+
}
24+
};
25+
26+
setupKeepAliveConnection();
27+
setupFirefoxWakeInterval();

0 commit comments

Comments
 (0)