@@ -4,9 +4,40 @@ import DataStore from '@ember-data/store';
4
4
export default class Store extends DataStore {
5
5
constructor ( args ) {
6
6
super ( args ) ;
7
- this . lifetimes = new LifetimesService ( this , {
8
- apiCacheSoftExpires : 30_000 ,
9
- apiCacheHardExpires : 60_000
10
- } ) ;
7
+ // This is default configuration that would be set automatically be Ember Data
8
+ // this.lifetimes = new LifetimesService(this, {
9
+ // apiCacheSoftExpires: 30_000,
10
+ // apiCacheHardExpires: 60_000
11
+ // });
12
+ this . lifetimes = {
13
+ isHardExpired ( identifier ) {
14
+ const cached = this . store . cache . peekRequest ( identifier ) ;
15
+ const twentyMinutesInMs = 20 * 60 * 1000 ;
16
+
17
+ function isStale ( headers , expirationTime ) {
18
+ const date = headers . get ( 'date' ) ;
19
+
20
+ if ( ! date ) {
21
+ return true ;
22
+ }
23
+
24
+ const time = new Date ( date ) . getTime ( ) ;
25
+ const now = Date . now ( ) ;
26
+ const deadline = time + expirationTime ;
27
+
28
+ const result = now > deadline ;
29
+
30
+ return result ;
31
+ }
32
+
33
+ return ! cached || ! cached . response || isStale ( cached . response . headers , twentyMinutesInMs ) ;
34
+ } ,
35
+
36
+ isSoftExpired ( identifier ) {
37
+ const { downlink, effectiveType } = navigator . connection ;
38
+
39
+ return downlink > 0 && effectiveType === '4g' ;
40
+ }
41
+ }
11
42
}
12
43
}
0 commit comments