File tree Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -3,12 +3,32 @@ import { createAsyncResourceBundle, createSelector } from 'redux-bundler'
3
3
// Matches APP_IDLE and peer bandwidth update intervals
4
4
export const IDENTITY_REFRESH_INTERVAL_MS = 5000
5
5
6
+ // Debounce getPromise calls
7
+ let lastIdentityFetch = 0
8
+ let lastIdentityPromise = null
9
+
6
10
const bundle = createAsyncResourceBundle ( {
7
11
name : 'identity' ,
8
12
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
+ } ,
12
32
staleAfter : IDENTITY_REFRESH_INTERVAL_MS ,
13
33
persist : false ,
14
34
checkIfOnline : false
You can’t perform that action at this time.
0 commit comments