@@ -37,6 +37,23 @@ const isHttpResponse = (data: any): boolean => {
37
37
return data && ( data instanceof HttpResponse || orCondition ) ;
38
38
} ;
39
39
40
+ const isJsOrResponseType = ( data : any ) : boolean => {
41
+ const jsType =
42
+ data . type === 'undefined' ||
43
+ data . type === 'object' ||
44
+ data . type === 'boolean' ||
45
+ data . type === 'number' ||
46
+ data . type === 'bigint' ||
47
+ data . type === 'string' ||
48
+ data . type === 'symbol' ||
49
+ data . type === 'function' ;
50
+
51
+ const responseType =
52
+ data . type === 'response' ;
53
+
54
+ return responseType || jsType ;
55
+ }
56
+
40
57
@Injectable ( )
41
58
export class CacheService {
42
59
private ttl : number = 60 * 60 ; // one hour
@@ -151,6 +168,10 @@ export class CacheService {
151
168
throw new Error ( MESSAGES [ 1 ] ) ;
152
169
}
153
170
171
+ if ( Blob . name === data . constructor . name ) {
172
+ return this . saveBlobItem ( key , data , groupKey , ttl ) ;
173
+ }
174
+
154
175
const expires = new Date ( ) . getTime ( ) + ttl * 1000 ,
155
176
type = isHttpResponse ( data ) ? 'response' : typeof data ,
156
177
value = JSON . stringify ( data ) ;
@@ -171,7 +192,7 @@ export class CacheService {
171
192
* @param {number } [ttl] - TTL in seconds
172
193
* @return {Promise<any> } - saved data
173
194
*/
174
- async saveBlobItem (
195
+ private async saveBlobItem (
175
196
key : string ,
176
197
blob : any ,
177
198
groupKey : string = 'none' ,
@@ -306,25 +327,6 @@ export class CacheService {
306
327
return CacheService . decodeRawData ( data ) ;
307
328
}
308
329
309
- /**
310
- * @description Get blob item from cache with expire check and correct type assign
311
- * @param {string } key - Unique key
312
- * @return {Promise<any> } - promise that resolves with blob data from cache
313
- */
314
- async getBlobItem ( key : string ) : Promise < Blob > {
315
- if ( ! this . cacheEnabled ) {
316
- throw new Error ( MESSAGES [ 1 ] ) ;
317
- }
318
-
319
- let data = await this . getRawItem ( key ) ;
320
-
321
- if ( data . expires < new Date ( ) . getTime ( ) && ( this . invalidateOffline || this . isOnline ( ) ) ) {
322
- throw new Error ( MESSAGES [ 2 ] + key ) ;
323
- }
324
-
325
- return CacheService . decodeRawBlobData ( data ) ;
326
- }
327
-
328
330
async getOrSetItem < T > (
329
331
key : string ,
330
332
factory : CacheValueFactory < T > ,
@@ -348,35 +350,28 @@ export class CacheService {
348
350
* @param {any } data - Data
349
351
* @return {any } - decoded data
350
352
*/
351
- static decodeRawData ( data : StorageCacheItem ) : any {
353
+ static async decodeRawData ( data : StorageCacheItem ) : Promise < any > {
352
354
let dataJson = JSON . parse ( data . value ) ;
353
- if ( isHttpResponse ( dataJson ) ) {
354
- let response : any = {
355
- body : dataJson . _body || dataJson . body ,
356
- status : dataJson . status ,
357
- headers : dataJson . headers ,
358
- statusText : dataJson . statusText ,
359
- url : dataJson . url
360
- } ;
361
-
362
- return new HttpResponse ( response ) ;
363
- }
364
-
365
- return dataJson ;
366
- }
367
-
368
- /**
369
- * @description Decode raw blob data from DB
370
- * @param {any } data - Data
371
- * @return {Promise<Blob> } - promise that resolves with a Blob.
372
- */
373
- static async decodeRawBlobData ( data : StorageCacheItem ) : Promise < Blob > {
374
- const dataURL = JSON . parse ( data . value ) ;
355
+ if ( isJsOrResponseType ( data ) ) {
356
+ if ( isHttpResponse ( dataJson ) ) {
357
+ let response : any = {
358
+ body : dataJson . _body || dataJson . body ,
359
+ status : dataJson . status ,
360
+ headers : dataJson . headers ,
361
+ statusText : dataJson . statusText ,
362
+ url : dataJson . url
363
+ } ;
364
+
365
+ return new HttpResponse ( response ) ;
366
+ }
375
367
376
- // Technique derived from: https://stackoverflow.com/a/36183085
377
- const response = await fetch ( dataURL ) ;
368
+ return dataJson ;
369
+ } else {
370
+ // Technique derived from: https://stackoverflow.com/a/36183085
371
+ const response = await fetch ( dataJson ) ;
378
372
379
- return response . blob ( ) ;
373
+ return response . blob ( ) ;
374
+ }
380
375
}
381
376
382
377
/**
@@ -470,8 +465,8 @@ export class CacheService {
470
465
} )
471
466
. catch ( e => {
472
467
this . getRawItem < T > ( key )
473
- . then ( res => {
474
- let result = CacheService . decodeRawData ( res ) ;
468
+ . then ( async ( res ) => {
469
+ let result = await CacheService . decodeRawData ( res ) ;
475
470
if ( metaKey ) {
476
471
result [ metaKey ] = result [ metaKey ] || { } ;
477
472
result [ metaKey ] . fromCache = true ;
@@ -485,42 +480,6 @@ export class CacheService {
485
480
return observableSubject . asObservable ( ) ;
486
481
}
487
482
488
- /**
489
- * @description Load blob item from cache if it's in cache or load from origin observable
490
- * @param {string } key - Unique key
491
- * @param {any } observable - Observable with blob data
492
- * @param {string } [groupKey] - group key
493
- * @param {number } [ttl] - TTL in seconds
494
- * @return {Observable<any> } - blob data from cache or origin observable
495
- */
496
- loadFromBlobObservable (
497
- key : string ,
498
- observable : Observable < Blob > ,
499
- groupKey ?: string ,
500
- ttl ?: number
501
- ) : Observable < Blob > {
502
- if ( ! this . cacheEnabled ) return observable ;
503
-
504
- observable = observable . pipe ( share ( ) ) ;
505
-
506
- return defer ( ( ) => {
507
- return from ( this . getBlobItem ( key ) ) . pipe (
508
- catchError ( e => {
509
- observable . subscribe (
510
- blob => {
511
- return this . saveBlobItem ( key , blob , groupKey , ttl ) ;
512
- } ,
513
- error => {
514
- return throwError ( error ) ;
515
- }
516
- ) ;
517
-
518
- return observable ;
519
- } )
520
- ) ;
521
- } ) ;
522
- }
523
-
524
483
/**
525
484
* Perform complete cache clear
526
485
* @return {Promise<any> }
0 commit comments