1
1
import { createAsyncThunk , createSelector , createSlice } from "@reduxjs/toolkit" ;
2
2
3
- const CACHE_NAME = "v1" ;
4
3
// Delete all caches older than 6 hours
5
4
const cacheRefreshIntervalInMilliseconds = 6 * 3600 * 1000 ;
6
5
7
6
export const poolsSlice = createSlice ( {
8
7
name : 'pools' ,
9
8
initialState : { list : [ ] } ,
10
- reducers : {
11
- refreshPoolsCaches : ( state ) => {
12
- const now = Date . now ( ) ;
13
- for ( const entry of state . list . map ( p => ( { date : p . date , url : poolTickersUrl ( p . aggregator ) } ) ) ) {
14
- const millisecondsSinceLastRefresh = now - entry . date ;
15
-
16
- if ( millisecondsSinceLastRefresh > cacheRefreshIntervalInMilliseconds ) {
17
- caches . open ( CACHE_NAME ) . then ( async ( cache ) => {
18
- const deletionSuccessful = await cache . delete ( entry . url ) ;
19
- if ( ! deletionSuccessful ) {
20
- console . error ( `Failed to delete cache: ${ entry . url } ` ) ;
21
- }
22
- } ) ;
23
- }
24
- }
25
- }
26
- } ,
9
+ reducers : { } ,
27
10
extraReducers : builder => builder . addCase ( updatePoolsForAggregator . fulfilled , ( state , action ) => {
28
- 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 ) ;
29
15
30
16
if ( existing ) {
31
17
existing . network = action . payload . network ;
32
18
existing . pools = action . payload . pools ;
19
+ existing . date = action . payload . date ;
33
20
} else {
34
21
state . list . push ( {
35
22
aggregator : action . payload . aggregator ,
@@ -41,61 +28,36 @@ export const poolsSlice = createSlice({
41
28
} )
42
29
} ) ;
43
30
44
- export const {
45
- refreshPoolsCaches
46
- } = poolsSlice . actions ;
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 ( ) ;
47
35
48
- const poolTickersUrl = aggregator => ` ${ aggregator } /signers/tickers` ;
36
+ const millisecondsSinceLastRefresh = now - ( aggregatorPools ?. date ?? 0 ) ;
49
37
50
- export const updatePoolsForAggregator = createAsyncThunk ( 'pools/updateForAggregator' , aggregator => {
51
- const url = poolTickersUrl ( aggregator ) ;
52
- return caches . match ( url )
53
- . then ( cached => {
54
- // Cache hit
55
- if ( cached ) {
56
- return cached ;
57
- }
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
+ }
58
50
59
- // Cache miss
60
- return fetch ( url ) . then ( response => {
61
- if ( response . status === 200 ) {
62
- caches . open ( CACHE_NAME ) . then ( async ( cache ) => {
63
- const putSuccessful = await cache . put ( url , response ) ;
64
- if ( ! putSuccessful ) {
65
- console . error ( `Failed to put cache: ${ url } ` ) ;
66
- }
67
- } ) ;
68
- }
69
- return response . clone ( ) ;
70
- } )
71
- } )
72
- . then ( response => response . status === 200 ? response . json ( ) : { } )
73
- . then ( data => {
74
- return {
75
- aggregator : aggregator ,
76
- date : Date . now ( ) ,
77
- network : data . network ,
78
- pools : data . signers ?? [ ] ,
79
- } ;
80
- } ) ;
51
+ return { keep_cached_data : true } ;
81
52
} ) ;
82
53
83
- const poolsForAggregator = createSelector ( [
84
- state => state . pools ,
85
- ( state , aggregator ) => aggregator
86
- ] ,
87
- ( pools , aggregator ) => {
88
- return pools . list . find ( poolsData => poolsData . aggregator === aggregator ) ;
89
- }
90
- ) ;
54
+ const poolsForAggregator = ( poolsSlice , aggregator ) => {
55
+ return poolsSlice . list . find ( poolsData => poolsData . aggregator === aggregator ) ;
56
+ }
91
57
92
58
export const getPool = createSelector ( [
93
59
state => state . pools ,
94
- ( state , aggregator , poolId ) => {
95
- return {
96
- aggregator : aggregator ,
97
- poolId : poolId
98
- } } ,
60
+ ( state , aggregator , poolId ) => ( { aggregator : aggregator , poolId : poolId , } ) ,
99
61
] ,
100
62
( pools , args ) => {
101
63
const aggregator = poolsForAggregator ( pools , args . aggregator ) ;
0 commit comments