Skip to content

Commit 236696e

Browse files
committed
fix: implement ping-pong mechanism to keep service worker alive
The service worker was becoming inactive when browser tabs lost focus. Implemented a more reliable ping-pong mechanism that sends regular ping messages from all UI contexts to the background service, preventing the browser from considering the service worker inactive.
1 parent b91f378 commit 236696e

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

apps/browser-extension-wallet/src/dapp-connector.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { render } from 'react-dom';
44
import { DappConnectorView } from '@routes';
55
import { StoreProvider } from '@stores';
66
import '@lib/i18n';
7+
import '@lib/scripts/keep-alive-ui';
78
import 'antd/dist/antd.css';
89
import { CurrencyStoreProvider } from '@providers/currency';
910
import { DatabaseProvider, AppSettingsProvider, AnalyticsProvider, ExternalLinkOpenerProvider } from '@providers';

apps/browser-extension-wallet/src/lib/scripts/background/services/utilityServices.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ export const exposeBackgroundService = (wallet$: Observable<ActiveWallet>): void
319319
},
320320
getAppVersion,
321321
backendFailures$,
322-
unhandledError$
322+
unhandledError$,
323+
ping: async () => 'pong' as const
323324
}),
324325
baseChannel: BaseChannels.BACKGROUND_ACTIONS,
325326
properties: backgroundServiceProperties
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
import { consumeRemoteApi } from '@cardano-sdk/web-extension';
2+
import { backgroundServiceProperties } from './background/config';
3+
import { BackgroundService, BaseChannels } from './types';
14
import { runtime } from 'webextension-polyfill';
25
import { logger } from '@lace/common';
36

4-
const TIMEOUT_DURATION = 5000;
7+
const PING_INTERVAL = 5000; // 5 seconds - same as original
58

6-
const setupKeepAliveConnection = () => {
7-
const port = runtime.connect({ name: 'keepAlive' });
8-
port.onDisconnect.addListener(setupKeepAliveConnection);
9-
};
9+
const backgroundService = consumeRemoteApi<BackgroundService>(
10+
{
11+
baseChannel: BaseChannels.BACKGROUND_ACTIONS,
12+
properties: backgroundServiceProperties
13+
},
14+
{ runtime, logger }
15+
);
1016

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-
}
17+
const keepServiceWorkerAlive = () => {
18+
setInterval(async () => {
19+
try {
20+
const response = await backgroundService.ping();
21+
logger.debug('Keep-alive ping response:', response);
22+
} catch (error) {
23+
logger.error('Keep-alive ping failed:', error);
24+
}
25+
}, PING_INTERVAL);
2426
};
2527

26-
setupKeepAliveConnection();
27-
setupFirefoxWakeInterval();
28+
// Start the keep-alive mechanism
29+
keepServiceWorkerAlive();

apps/browser-extension-wallet/src/lib/scripts/types/background-service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export type BackgroundService = {
112112
getAppVersion: () => Promise<string>;
113113
backendFailures$: BehaviorSubject<number>;
114114
unhandledError$: Observable<UnhandledError>;
115+
ping: () => Promise<'pong'>;
115116
};
116117

117118
export type WalletMode = {

apps/browser-extension-wallet/src/popup.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { StoreProvider } from '@stores';
77
import { CurrencyStoreProvider } from '@providers/currency';
88
import { AppSettingsProvider, DatabaseProvider, ThemeProvider, AnalyticsProvider } from '@providers';
99
import '@lib/i18n';
10+
import '@lib/scripts/keep-alive-ui';
1011
import 'antd/dist/antd.css';
1112
import './styles/index.scss';
1213
import 'normalize.css';

0 commit comments

Comments
 (0)