Skip to content

Commit 2d2e976

Browse files
committed
fix: stop spamming kubo node with /id calls
1 parent d116bc7 commit 2d2e976

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/bundles/identity.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@ import { createAsyncResourceBundle, createSelector } from 'redux-bundler'
33
// Matches APP_IDLE and peer bandwidth update intervals
44
export const IDENTITY_REFRESH_INTERVAL_MS = 5000
55

6+
// Debounce getPromise calls
7+
let lastIdentityFetch = 0
8+
let lastIdentityPromise = null
9+
610
const bundle = createAsyncResourceBundle({
711
name: 'identity',
812
actionBaseType: 'IDENTITY',
9-
getPromise: ({ getIpfs }) => getIpfs().id().catch((err) => {
10-
console.error('Failed to get identity', err)
11-
}),
13+
getPromise: ({ getIpfs }) => {
14+
const now = Date.now()
15+
const timeSinceLastFetch = now - lastIdentityFetch
16+
17+
// If we recently fetched and have a cached promise, return it
18+
if (timeSinceLastFetch < IDENTITY_REFRESH_INTERVAL_MS && lastIdentityPromise != null) {
19+
return lastIdentityPromise
20+
}
21+
22+
lastIdentityFetch = now
23+
lastIdentityPromise = getIpfs().id().catch((err) => {
24+
console.error('Failed to get identity: ', err)
25+
// Clear cached promise on error so we can retry
26+
lastIdentityPromise = null
27+
throw err
28+
})
29+
30+
return lastIdentityPromise
31+
},
1232
staleAfter: IDENTITY_REFRESH_INTERVAL_MS,
1333
persist: false,
1434
checkIfOnline: false

0 commit comments

Comments
 (0)