-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { supportedChainToChain, updateChainStore } from '@/utils/chain'; | ||
import { isManifestV3 } from '@/utils/env'; | ||
import { Chain } from '@debank/common'; | ||
import browser from 'webextension-polyfill'; | ||
import { ALARMS_SYNC_CHAINS } from '../utils/alarms'; | ||
import { http } from '../utils/http'; | ||
import { SupportedChain } from './openapi'; | ||
|
||
class SyncChainService { | ||
timer: ReturnType<typeof setInterval> | null = null; | ||
|
||
syncMainnetChainList = async () => { | ||
try { | ||
const chains = await http | ||
.get('https://static.debank.com/supported_chains.json') | ||
.then((res) => { | ||
return res.data as SupportedChain[]; | ||
}); | ||
const list: Chain[] = chains | ||
.filter((item) => !item.is_disabled) | ||
.map((item) => { | ||
const chain: Chain = supportedChainToChain(item); | ||
return chain; | ||
}); | ||
updateChainStore({ | ||
mainnetList: list, | ||
}); | ||
chrome.storage.local.set({ | ||
rabbyMainnetChainList: list, | ||
}); | ||
} catch (e) { | ||
console.error('fetch chain list error: ', e); | ||
} | ||
}; | ||
|
||
resetTimer = () => { | ||
const periodInMinutes = 60; | ||
if (this.timer) { | ||
clearTimeout(this.timer); | ||
} else if (isManifestV3) { | ||
browser.alarms.clear(ALARMS_SYNC_CHAINS); | ||
} | ||
|
||
if (isManifestV3) { | ||
browser.alarms.create(ALARMS_SYNC_CHAINS, { | ||
delayInMinutes: periodInMinutes, | ||
periodInMinutes: periodInMinutes, | ||
}); | ||
browser.alarms.onAlarm.addListener((alarm) => { | ||
if (alarm.name === ALARMS_SYNC_CHAINS) { | ||
this.syncMainnetChainList(); | ||
} | ||
}); | ||
} else { | ||
this.timer = setInterval(() => { | ||
this.syncMainnetChainList(); | ||
}, periodInMinutes * 60 * 1000); | ||
} | ||
}; | ||
roll = () => { | ||
this.resetTimer(); | ||
}; | ||
} | ||
|
||
export const syncChainService = new SyncChainService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const ALARMS_AUTO_LOCK = 'ALARMS_AUTO_LOCK'; | ||
export const ALARMS_RPC_CACHE = 'ALARMS_RPC_CACHE'; | ||
export const ALARMS_RELOAD_TX = 'ALARMS_RELOAD_TX'; | ||
export const ALARMS_SYNC_CHAINS = 'ALARMS_SYNC_CHAINS'; |