-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathFioService.ts
More file actions
161 lines (137 loc) · 4.99 KB
/
Copy pathFioService.ts
File metadata and controls
161 lines (137 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import type { EdgeAccount, EdgeCurrencyWallet } from 'edge-core-js'
import * as React from 'react'
import { showFioExpiredModal } from '../../actions/FioActions'
import { FIO_WALLET_TYPE } from '../../constants/WalletAndCurrencyConstants'
import { useAsyncEffect } from '../../hooks/useAsyncEffect'
import { useHandler } from '../../hooks/useHandler'
import { useWatch } from '../../hooks/useWatch'
import { useDispatch, useSelector } from '../../types/reactRedux'
import type { NavigationBase } from '../../types/routerTypes'
import type { FioDomain } from '../../types/types'
import {
getExpiredSoonFioDomains,
getFioExpiredCheckFromDisklet,
needToCheckExpired,
refreshFioNames,
setFioExpiredCheckToDisklet
} from '../../util/FioAddressUtils'
import { makePeriodicTask } from '../../util/PeriodicTask'
import { showDevError } from './AirshipInstance'
const EXPIRE_CHECK_TIMEOUT = 30000
interface Props {
account: EdgeAccount
navigation: NavigationBase
}
type NameDates = Record<string, Date>
export const FioService: React.FC<Props> = props => {
const { account, navigation } = props
const dispatch = useDispatch()
const currencyWallets = useWatch(account, 'currencyWallets')
const expiredLastChecks = React.useRef<NameDates | undefined>(undefined)
const expireReminderShown = React.useRef(false)
const expiredChecking = React.useRef(false)
const walletsCheckedForExpired = React.useRef<Record<string, boolean>>({})
const fioWallets = React.useRef<EdgeCurrencyWallet[]>([])
const disklet = useSelector(state => state.core.disklet)
React.useEffect(() => {
const fioWalletsTemp: EdgeCurrencyWallet[] = []
for (const walletId of Object.keys(currencyWallets)) {
if (currencyWallets[walletId].type === FIO_WALLET_TYPE) {
fioWalletsTemp.push(currencyWallets[walletId])
}
}
if (!arraysEqual(fioWalletsTemp, fioWallets.current)) {
fioWallets.current = fioWalletsTemp
dispatch({
type: 'UPDATE_FIO_WALLETS',
data: { fioWallets: fioWallets.current }
})
}
}, [currencyWallets, dispatch])
const refreshNamesToCheckExpired = useHandler(async () => {
if (expireReminderShown.current) return
if (fioWallets.current.length === 0) {
return
}
if (expiredChecking.current) return
// Wallet objects can exist before their engines do (wallet cache),
// and refreshFioNames calls engine-backed otherMethods.
// Skip pre-engine wallets and let the next cycle retry them:
const readyWallets = fioWallets.current.filter(
fioWallet => fioWallet.otherMethods.getFioAddresses != null
)
if (readyWallets.length === 0) return
expiredChecking.current = true
try {
const walletsToCheck: EdgeCurrencyWallet[] = []
for (const fioWallet of readyWallets) {
if (!walletsCheckedForExpired.current[fioWallet.id]) {
walletsToCheck.push(fioWallet)
}
}
const namesToCheck: FioDomain[] = []
const { fioDomains, fioWalletsById } = await refreshFioNames(
walletsToCheck
)
expiredLastChecks.current ??= await getFioExpiredCheckFromDisklet(disklet)
for (const fioDomain of fioDomains) {
if (needToCheckExpired(expiredLastChecks.current, fioDomain.name)) {
namesToCheck.push(fioDomain)
}
}
if (namesToCheck.length !== 0) {
const expired: FioDomain[] = getExpiredSoonFioDomains(fioDomains)
if (expired.length > 0) {
const first: FioDomain = expired[0]
const fioWallet: EdgeCurrencyWallet = fioWalletsById[first.walletId]
await showFioExpiredModal(navigation, fioWallet, first)
expireReminderShown.current = true
expiredLastChecks.current[first.name] = new Date()
await setFioExpiredCheckToDisklet(expiredLastChecks.current, disklet)
}
for (const walletId in fioWalletsById) {
walletsCheckedForExpired.current[walletId] = true
}
}
} finally {
// Always release the latch, or a cycle with nothing to check
// (or an error) would disable this check for the whole session:
expiredChecking.current = false
}
})
// Check for expired FIO domains
useAsyncEffect(
async () => {
await account.waitForAllWallets()
const task = makePeriodicTask(
refreshNamesToCheckExpired,
EXPIRE_CHECK_TIMEOUT,
{
onError(e: unknown) {
console.error('refreshNamesToCheckExpired error:', e)
showDevError(e)
}
}
)
task.start()
return () => {
task.stop()
}
},
[refreshNamesToCheckExpired],
'FioService:checkExpired'
)
return null
}
function arraysEqual(
arr1: EdgeCurrencyWallet[],
arr2: EdgeCurrencyWallet[]
): boolean {
if (arr1.length !== arr2.length) return false
arr1.sort((a, b) => a.id.localeCompare(b.id))
arr2.sort((a, b) => a.id.localeCompare(b.id))
for (let i = 0; i < arr1.length; i++) {
if (arr1[i].id !== arr2[i].id) return false
}
return true
}