1
1
import { createAsyncThunk , createSelector , createSlice } from "@reduxjs/toolkit" ;
2
2
3
+ // Delete all caches older than 6 hours
4
+ const cacheRefreshIntervalInMilliseconds = 6 * 3600 * 1000 ;
5
+
3
6
export const poolsSlice = createSlice ( {
4
7
name : 'pools' ,
5
8
initialState : { list : [ ] } ,
6
9
reducers : { } ,
7
10
extraReducers : builder => builder . addCase ( updatePoolsForAggregator . fulfilled , ( state , action ) => {
8
- let existing = poolsForAggregator ( state , action . payload . aggregator ) ;
11
+ if ( action . payload . keep_cached_data ) {
12
+ return ;
13
+ }
14
+ const existing = poolsForAggregator ( state , action . payload . aggregator ) ;
9
15
10
16
if ( existing ) {
11
- existing . date = action . payload . date ;
17
+ existing . network = action . payload . network ;
12
18
existing . pools = action . payload . pools ;
19
+ existing . date = action . payload . date ;
13
20
} else {
14
21
state . list . push ( {
15
22
aggregator : action . payload . aggregator ,
@@ -21,30 +28,36 @@ export const poolsSlice = createSlice({
21
28
} )
22
29
} ) ;
23
30
24
- export const updatePoolsForAggregator = createAsyncThunk ( 'pools/updateForAggregator' , aggregator => {
25
- return fetch ( `${ aggregator } /signers/tickers` )
26
- . then ( response => response . status === 200 ? response . json ( ) : { } )
27
- . then ( data => {
28
- return {
29
- aggregator : aggregator ,
30
- date : Date . now ( ) ,
31
- network : data . network ,
32
- pools : data . signers ,
33
- } ;
34
- } ) ;
31
+ export const updatePoolsForAggregator = createAsyncThunk ( 'pools/updateForAggregator' , ( aggregator , thunkAPI ) => {
32
+ const state = thunkAPI . getState ( ) ;
33
+ const aggregatorPools = poolsForAggregator ( state . pools , aggregator ) ;
34
+ const now = Date . now ( ) ;
35
+
36
+ const millisecondsSinceLastRefresh = now - ( aggregatorPools ?. date ?? 0 ) ;
37
+
38
+ if ( millisecondsSinceLastRefresh > cacheRefreshIntervalInMilliseconds ) {
39
+ return fetch ( `${ aggregator } /signers/tickers` )
40
+ . then ( response => response . status === 200 ? response . json ( ) : { } )
41
+ . then ( data => {
42
+ return {
43
+ aggregator : aggregator ,
44
+ date : now ,
45
+ network : data . network ,
46
+ pools : data . signers ?? [ ] ,
47
+ } ;
48
+ } ) ;
49
+ }
50
+
51
+ return { keep_cached_data : true } ;
35
52
} ) ;
36
53
37
- const poolsForAggregator = ( state , aggregator ) => {
38
- return state . list . find ( poolsData => poolsData . aggregator === aggregator ) ;
39
- } ;
54
+ const poolsForAggregator = ( poolsSlice , aggregator ) => {
55
+ return poolsSlice . list . find ( poolsData => poolsData . aggregator === aggregator ) ;
56
+ }
40
57
41
58
export const getPool = createSelector ( [
42
59
state => state . pools ,
43
- ( state , aggregator , poolId ) => {
44
- return {
45
- aggregator : aggregator ,
46
- poolId : poolId
47
- } } ,
60
+ ( state , aggregator , poolId ) => ( { aggregator : aggregator , poolId : poolId , } ) ,
48
61
] ,
49
62
( pools , args ) => {
50
63
const aggregator = poolsForAggregator ( pools , args . aggregator ) ;
0 commit comments