@@ -4,16 +4,16 @@ import {Observable, Subject} from 'rxjs/Rx';
4
4
import { Request , Response , ResponseOptions } from '@angular/http' ;
5
5
6
6
const MESSAGES = {
7
- 0 : " Cache initialization error: " ,
8
- 1 : " Cache is not enabled." ,
9
- 2 : " Cache entry already expired: " ,
10
- 3 : " No such key: " ,
11
- 4 : " No enteries were deleted, because browser is offline."
12
- }
7
+ 0 : ' Cache initialization error: ' ,
8
+ 1 : ' Cache is not enabled.' ,
9
+ 2 : ' Cache entry already expired: ' ,
10
+ 3 : ' No such key: ' ,
11
+ 4 : ' No enteries were deleted, because browser is offline.'
12
+ } ;
13
13
14
14
@Injectable ( )
15
15
export class CacheService {
16
- private ttl : number = 60 * 60 ; //one hour
16
+ private ttl : number = 60 * 60 ; // one hour
17
17
private tableName : string = 'cache' ;
18
18
private cacheKeys : string [ ] = [ 'key unique' , 'value' , 'expire INTEGER' , 'type' , 'groupKey' ] ;
19
19
private storage : SqlStorage ;
@@ -123,12 +123,14 @@ export class CacheService {
123
123
let expire = new Date ( ) . getTime ( ) + ( ttl * 1000 ) ;
124
124
let type = CacheService . isRequest ( data ) ? 'request' : typeof data ;
125
125
let value = JSON . stringify ( data ) ;
126
- const valuesMap = { key, value, expire, type, groupKey }
127
- const values = Object . keys ( valuesMap ) . map ( key => valuesMap [ key ] )
126
+ const valuesMap = { key, value, expire, type, groupKey } ;
127
+ const values = Object . keys ( valuesMap ) . map ( key => `'${ valuesMap [ key ] } '` ) ;
128
+
129
+ let query = `INSERT OR REPLACE INTO ${ this . tableName } (${ Object . keys ( valuesMap ) . join ( ', ' ) } ) VALUES (${ values . join ( ', ' ) } )` ;
128
130
129
- let query = `INSERT OR REPLACE INTO ${ this . tableName } ( ${ Object . keys ( valuesMap ) . join ( ', ' ) } ) VALUES ( ${ Object . keys ( valuesMap ) . fill ( '?' ) . join ( ', ' ) } )` ;
131
+ console . log ( query ) ;
130
132
131
- return this . storage . query ( query , values ) . then ( ( ) => data ) ;
133
+ return this . storage . query ( query ) . then ( ( ) => data ) ;
132
134
}
133
135
134
136
/**
@@ -175,8 +177,8 @@ export class CacheService {
175
177
}
176
178
177
179
return this . getRawItem ( key ) . then ( data => {
178
- if ( data . expire < new Date ( ) . getTime ( ) ) {
179
- if ( this . invalidateOffline ) {
180
+ if ( data . expire < new Date ( ) . getTime ( ) ) {
181
+ if ( this . invalidateOffline ) {
180
182
return Promise . reject ( MESSAGES [ 2 ] + key ) ;
181
183
} else if ( this . isOnline ( ) ) {
182
184
return Promise . reject ( MESSAGES [ 2 ] + key ) ;
@@ -236,31 +238,31 @@ export class CacheService {
236
238
* @param {number } [ttl] - TTL in seconds
237
239
* @return {Observable<any> } - data from cache or origin observable
238
240
*/
239
- public loadFromDelayedObservable ( key : string , observable : any , groupKey ?: string , ttl : number = this . ttl , delayType : string = " expired" ) : Observable < any > {
241
+ public loadFromDelayedObservable ( key : string , observable : any , groupKey ?: string , ttl : number = this . ttl , delayType : string = ' expired' ) : Observable < any > {
240
242
if ( ! this . enableCache ) return observable ;
241
243
242
244
let observableSubject = new Subject ( ) ;
243
245
observable = observable . share ( ) ;
244
246
245
247
let subscribeOrigin = ( ) => {
246
248
observable . subscribe ( res => {
247
- this . saveItem ( key , res , groupKey , ttl )
249
+ this . saveItem ( key , res , groupKey , ttl ) ;
248
250
observableSubject . next ( res ) ;
249
251
} , null , ( ) => {
250
252
observableSubject . complete ( ) ;
251
253
} ) ;
252
- }
254
+ } ;
253
255
254
256
this . getItem ( key ) . then ( ( data ) => {
255
257
observableSubject . next ( data ) ;
256
- if ( delayType == 'all' ) {
258
+ if ( delayType === 'all' ) {
257
259
subscribeOrigin ( ) ;
258
260
}
259
261
} , ( e ) => {
260
262
this . getRawItem ( key ) . then ( res => {
261
263
observableSubject . next ( CacheService . decodeRawData ( res ) ) ;
262
264
} ) . then ( ( ) => {
263
- subscribeOrigin ( )
265
+ subscribeOrigin ( ) ;
264
266
} ) . catch ( ( ) => subscribeOrigin ( ) ) ;
265
267
} ) ;
266
268
@@ -289,7 +291,7 @@ export class CacheService {
289
291
return Promise . reject ( MESSAGES [ 2 ] ) ;
290
292
}
291
293
292
- if ( ! this . isOnline ( ) && ! ignoreOnlineStatus ) {
294
+ if ( ! this . isOnline ( ) && ! ignoreOnlineStatus ) {
293
295
return Promise . reject ( MESSAGES [ 4 ] ) ;
294
296
}
295
297
@@ -316,7 +318,7 @@ export class CacheService {
316
318
* @return {boolean } - data from cache
317
319
*/
318
320
public static isRequest ( data : any ) : boolean {
319
- if ( data instanceof Request || ( typeof data == 'object' && data . hasOwnProperty ( '_body' ) && data . hasOwnProperty ( 'status' ) &&
321
+ if ( data instanceof Request || ( typeof data === 'object' && data . hasOwnProperty ( '_body' ) && data . hasOwnProperty ( 'status' ) &&
320
322
data . hasOwnProperty ( 'statusText' ) && data . hasOwnProperty ( 'type' ) && data . hasOwnProperty ( 'headers' )
321
323
&& data . hasOwnProperty ( 'url' ) ) ) {
322
324
return true ;
0 commit comments