Skip to content

Commit 9d2fe2d

Browse files
feat: improve and refactor code background code structure
1 parent b13caed commit 9d2fe2d

29 files changed

+417
-485
lines changed

source/components/Header/Menus/NetworkMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import ethIcon from 'assets/images/ethIcon.svg';
1313
import { Icon } from 'components/index';
1414
import { useUtils } from 'hooks/index';
1515
import { useController } from 'hooks/useController';
16-
import { dispatchChangeNetworkBgEvent } from 'scripts/Background';
16+
import { dispatchChangeNetworkBgEvent } from 'scripts/Background/utils/bgActions';
1717
import { RootState } from 'state/store';
1818
import { NetworkType } from 'utils/types';
1919

source/components/KeepAliveContainer/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useRef } from 'react';
22

3-
import { keepSWAlive } from 'scripts/Background';
3+
import { keepSWAlive } from 'scripts/Background/utils/bgActions';
44

55
export const KeepAliveContainer = () => {
66
const timer = useRef<undefined | NodeJS.Timer>(undefined);

source/config/consts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const MV3_OPTIONS = {
8484
description: 'A Non-Custodial Crypto Wallet',
8585
short_name: 'pali',
8686
permissions: [
87+
'alarms',
8788
'storage',
8889
'activeTab',
8990
'clipboardWrite',

source/pages/Settings/AutoLock.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ const AutoLockView = () => {
2626

2727
useEffect(() => {
2828
form.setFieldsValue({ minutes: timer });
29-
if (isEnabled) {
30-
startInactivityTimer(timer);
31-
}
3229
}, [timer, form, isEnabled]);
3330

3431
const handleMaxClick = () => {
@@ -51,6 +48,7 @@ const AutoLockView = () => {
5148
} else {
5249
setHasError(false);
5350
}
51+
startInactivityTimer(autolockMinutes);
5452
controllerEmitter(['wallet', 'setAutolockTimer'], [autolockMinutes]);
5553
controllerEmitter(['wallet', 'setIsAutolockEnabled'], [isEnabled]);
5654
setConfirmed(true);

source/pages/Settings/Languages.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useNavigate } from 'react-router-dom';
66

77
import checked from 'assets/icons/greenChecked.svg';
88
import { DefaultModal, Layout, NeutralButton } from 'components/index';
9-
import { setLanguageInLocalStorage } from 'scripts/Background';
9+
import { setLanguageInLocalStorage } from 'scripts/Background/utils/bgActions';
1010
import { RootState } from 'state/store';
1111
import { i18next } from 'utils/i18n';
1212
import { chromeStorage } from 'utils/storageAPI';

source/routers/useRouterLogic.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { useLocation } from 'react-router-dom';
55

66
import { useUtils } from 'hooks/index';
77
import { useController } from 'hooks/useController';
8+
import { controllerEmitter } from 'scripts/Background/controllers/controllerEmitter';
9+
import { startInactivityTimer } from 'scripts/Background/events/InactivityTimer';
810
import {
911
removeVerifyPaliRequestListener,
1012
resetPaliRequestsCount,
1113
verifyPaliRequests,
12-
} from 'scripts/Background';
13-
import { controllerEmitter } from 'scripts/Background/controllers/controllerEmitter';
14-
import { startInactivityTimer } from 'scripts/Background/events/InactivityTimer';
14+
} from 'scripts/Background/utils/bgActions';
1515
import { rehydrateStore } from 'state/rehydrate';
1616
import store, { RootState } from 'state/store';
1717
import { SYS_UTXO_MAINNET_NETWORK } from 'utils/constants';
Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
export const startInactivityTimer = (autolockMinutes: number) => {
1+
export const startInactivityTimer = async (autolockMinutes: number) => {
22
const autoLockMilliseconds = autolockMinutes * 60 * 1000;
3-
let lastActivityTimestamp = Date.now();
43

5-
const resetTimer = () => {
6-
lastActivityTimestamp = Date.now();
7-
};
4+
await chrome.storage.local.set({ autoLockMilliseconds });
85

9-
const checkInactivity = () => {
10-
const currentTime = Date.now();
11-
if (currentTime - lastActivityTimestamp >= autoLockMilliseconds) {
12-
chrome.runtime.sendMessage({
13-
type: 'lock_wallet',
14-
target: 'background',
15-
});
16-
}
6+
const resetTimer = () => {
7+
chrome.storage.local.set({ lastActivityTimestamp: Date.now() });
178
};
189

1910
// DOM Events
@@ -22,6 +13,22 @@
2213
events.forEach((event) => {
2314
document.addEventListener(event, resetTimer);
2415
});
16+
await chrome.alarms.clear('check_inactivity');
17+
await chrome.alarms.create('check_inactivity', { periodInMinutes: 0.5 });
18+
};
19+
20+
export const checkInactivity = async () => {
21+
const { lastActivityTimestamp, autoLockMilliseconds } =
22+
await chrome.storage.local.get([
23+
'lastActivityTimestamp',
24+
'autoLockMilliseconds',
25+
]);
2526

26-
setInterval(checkInactivity, 1000);
27+
const currentTime = Date.now();
28+
if (currentTime - lastActivityTimestamp >= autoLockMilliseconds) {
29+
await chrome.runtime.sendMessage({
30+
type: 'lock_wallet',
31+
target: 'background',
32+
});
33+
}
2734
};

source/scripts/Background/events/onConnect.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

source/scripts/Background/events/onInstalled.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

source/scripts/Background/events/onMessage.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)