File tree Expand file tree Collapse file tree 2 files changed +40
-11
lines changed Expand file tree Collapse file tree 2 files changed +40
-11
lines changed Original file line number Diff line number Diff line change @@ -4,9 +4,24 @@ import { inject as service } from '@ember/service';
4
4
5
5
export default RESTAdapter . extend ( {
6
6
fastboot : service ( ) ,
7
+ fetcher : service ( ) ,
7
8
8
9
namespace : 'api/v1' ,
9
10
11
+ ajax : function ( url , type , options ) {
12
+ if ( type === 'GET' ) {
13
+ let cache = this . fetcher . get ( url ) ;
14
+ if ( cache ) {
15
+ return cache ;
16
+ }
17
+ }
18
+
19
+ return this . _super ( url , type , options ) . then ( resp => {
20
+ this . fetcher . put ( url , resp ) ;
21
+ return resp ;
22
+ } ) ;
23
+ } ,
24
+
10
25
headers : computed ( 'fastboot.{isFastBoot,request.headers}' , function ( ) {
11
26
if ( this . fastboot . isFastBoot ) {
12
27
return { 'User-Agent' : this . fastboot . request . headers . get ( 'User-Agent' ) } ;
Original file line number Diff line number Diff line change @@ -2,26 +2,40 @@ import Service, { inject as service } from '@ember/service';
2
2
3
3
import ajax from '../utils/ajax' ;
4
4
5
+ const KEY = 'ajax-cache' ;
6
+
5
7
export default class FetcherService extends Service {
6
8
@service fastboot ;
7
9
8
- ajax ( url ) {
10
+ get ( url ) {
11
+ let shoebox = this . fastboot . shoebox ;
12
+ if ( ! shoebox ) {
13
+ return ;
14
+ }
15
+ let cache = shoebox . retrieve ( KEY ) || { } ;
16
+ return cache [ url ] ;
17
+ }
18
+
19
+ put ( url , obj ) {
9
20
let fastboot = this . fastboot ;
10
21
let shoebox = this . fastboot . shoebox ;
11
- let cache = shoebox . retrieve ( 'ajax-cache' ) ;
12
- if ( ! cache ) {
13
- cache = { } ;
22
+ if ( ! ( shoebox && fastboot . isFastBoot ) ) {
23
+ return ;
14
24
}
15
25
16
- if ( cache [ url ] ) {
17
- return cache [ url ] ;
26
+ let cache = shoebox . retrieve ( KEY ) || { } ;
27
+ cache [ url ] = deepCopy ( obj ) ;
28
+ shoebox . put ( KEY , cache ) ;
29
+ }
30
+
31
+ ajax ( url ) {
32
+ let resp = this . get ( url ) ;
33
+ if ( resp ) {
34
+ return resp ;
18
35
}
19
36
20
- return ajax ( url ) . then ( function ( resp ) {
21
- if ( shoebox && fastboot . isFastBoot ) {
22
- cache [ url ] = deepCopy ( resp ) ;
23
- shoebox . put ( 'ajax-cache' , cache ) ;
24
- }
37
+ return ajax ( url ) . then ( resp => {
38
+ this . put ( url , resp ) ;
25
39
return resp ;
26
40
} ) ;
27
41
}
You can’t perform that action at this time.
0 commit comments