1
1
import { Injectable } from '@angular/core' ;
2
- import { Storage , SqlStorage } from 'ionic-angular ' ;
2
+ import { SqlStorage } from './storage ' ;
3
3
import { Observable , Subject } from 'rxjs/Rx' ;
4
4
import { Request , Response , ResponseOptions } from '@angular/http' ;
5
5
@@ -8,23 +8,24 @@ const MESSAGES = {
8
8
1 : "Cache is not enabled." ,
9
9
2 : "Cache entry already expired: " ,
10
10
3 : "No such key: " ,
11
- 4 : "No enteries were deleted, because browser is offline." ,
11
+ 4 : "No enteries were deleted, because browser is offline."
12
12
}
13
13
14
14
@Injectable ( )
15
15
export class CacheService {
16
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
- private storage : Storage ;
19
+ private storage : SqlStorage ;
20
20
private enableCache : boolean = true ;
21
21
private invalidateOffline : boolean = false ;
22
22
private networkStatusChanges : Observable < boolean > ;
23
23
private networkStatus : boolean = true ;
24
24
25
25
constructor ( ) {
26
+ console . log ( 'constructor' ) ;
26
27
try {
27
- this . storage = new Storage ( SqlStorage ) ;
28
+ this . storage = new SqlStorage ( ) ;
28
29
this . watchNetworkInit ( ) ;
29
30
this . initDatabase ( ) ;
30
31
this . enableCache = true ;
@@ -154,14 +155,12 @@ export class CacheService {
154
155
}
155
156
156
157
let query = `SELECT * FROM ${ this . tableName } WHERE key = '${ key } '` ;
157
-
158
- return this . storage . query ( query ) . then ( data => {
159
- let result = data . res . rows . item ( 0 ) ;
160
- if ( ! result ) {
158
+ return this . storage . query ( query ) . then ( ( data : SQLResultSet ) => {
159
+ if ( data . rows . length === 0 || ! data . rows . item ( 0 ) ) {
161
160
return Promise . reject ( MESSAGES [ 3 ] + key ) ;
162
- } else {
163
- return result ;
164
161
}
162
+
163
+ return data . rows . item ( 0 ) ;
165
164
} ) ;
166
165
}
167
166
@@ -216,9 +215,9 @@ export class CacheService {
216
215
* @param {any } observable - Observable with data
217
216
* @param {string } [groupKey] - group key
218
217
* @param {number } [ttl] - TTL in seconds
219
- * @return {any } - data from cache or origin observable
218
+ * @return {Observable< any> } - data from cache or origin observable
220
219
*/
221
- public loadFromObservable ( key : string , observable : any , groupKey ?: string , ttl ?: number ) : any {
220
+ public loadFromObservable ( key : string , observable : any , groupKey ?: string , ttl ?: number ) : Observable < any > {
222
221
if ( ! this . enableCache ) return observable ;
223
222
224
223
observable = observable . share ( ) ;
@@ -235,9 +234,9 @@ export class CacheService {
235
234
* @param {any } observable - Observable with data
236
235
* @param {string } [groupKey] - group key
237
236
* @param {number } [ttl] - TTL in seconds
238
- * @return {any } - data from cache or origin observable
237
+ * @return {Observable< any> } - data from cache or origin observable
239
238
*/
240
- public loadFromDelayedObservable ( key : string , observable : any , groupKey ?: string , ttl : number = this . ttl , delayType : string = "expired" ) : any {
239
+ public loadFromDelayedObservable ( key : string , observable : any , groupKey ?: string , ttl : number = this . ttl , delayType : string = "expired" ) : Observable < any > {
241
240
if ( ! this . enableCache ) return observable ;
242
241
243
242
let observableSubject = new Subject ( ) ;
0 commit comments