@@ -7,8 +7,40 @@ export interface FolderThumbData {
77 folderContentsType : FolderContentsType ;
88}
99
10+ enum CachedThumbnailType {
11+ Image = 'image' ,
12+ Icon = 'icon' ,
13+ }
14+
15+ interface CachedThumbnailBase {
16+ type : CachedThumbnailType ;
17+ }
18+
19+ interface CachedImage extends CachedThumbnailBase {
20+ type : CachedThumbnailType . Image ;
21+ url : string ;
22+ }
23+
24+ interface CachedIcon extends CachedThumbnailBase {
25+ type : CachedThumbnailType . Icon ;
26+ icon : FolderContentsType ;
27+ }
28+
29+ type CachedThumbnail = CachedImage | CachedIcon ;
30+
31+ function isCachedThumbnail ( entry : unknown ) : entry is CachedThumbnail {
32+ return (
33+ typeof entry === 'object' &&
34+ entry !== null &&
35+ 'type' in entry &&
36+ Object . values ( CachedThumbnailType ) . includes (
37+ ( entry as CachedThumbnailBase ) . type ,
38+ )
39+ ) ;
40+ }
41+
1042export class ThumbnailCache {
11- private cache : Map < number , [ string , string ] > ;
43+ private cache : Map < number , CachedThumbnail > ;
1244 private readonly STORAGE_KEY : string = 'folderThumbnailCache' ;
1345
1446 constructor ( private storage : StorageService ) {
@@ -18,35 +50,38 @@ export class ThumbnailCache {
1850 public saveThumbnail ( item : ItemVO , thumbs : FolderThumbData ) : void {
1951 this . fetchCacheMapFromStorage ( ) ;
2052 if ( thumbs . folderContentsType === FolderContentsType . NORMAL ) {
21- this . cache . set ( item . folder_linkId , [
22- thumbs . folderThumb ,
23- '' ,
24- ] ) ;
53+ this . cache . set ( item . folder_linkId , {
54+ type : CachedThumbnailType . Image ,
55+ url : thumbs . folderThumb ,
56+ } ) ;
2557 } else {
26- this . cache . set ( item . folder_linkId , [ 'icon' , thumbs . folderContentsType ] ) ;
58+ this . cache . set ( item . folder_linkId , {
59+ type : CachedThumbnailType . Icon ,
60+ icon : thumbs . folderContentsType ,
61+ } ) ;
2762 }
2863 this . saveMapToStorage ( ) ;
2964 }
3065
3166 public getThumbnail ( item : ItemVO ) : FolderThumbData {
3267 if ( this . cache . has ( item . folder_linkId ) ) {
33- const thumbs = this . cache . get ( item . folder_linkId ) ;
34- if ( thumbs && Array . isArray ( thumbs ) && thumbs . length > 1 ) {
35- if ( thumbs [ 0 ] === 'icon' ) {
68+ const entry = this . cache . get ( item . folder_linkId ) ;
69+ if ( isCachedThumbnail ( entry ) ) {
70+ if ( entry . type === CachedThumbnailType . Image ) {
71+ return {
72+ folderThumb : entry . url ,
73+ folderContentsType : FolderContentsType . NORMAL ,
74+ } ;
75+ }
76+ if ( entry . type === CachedThumbnailType . Icon ) {
3677 return {
3778 folderThumb : '' ,
38- folderContentsType : thumbs [ 1 ] as FolderContentsType ,
79+ folderContentsType : entry . icon ,
3980 } ;
4081 }
41- // Cast to string just to be sure we actually have strings from our data structure.
42- return {
43- folderThumb : `${ thumbs [ 0 ] } ` ,
44- folderContentsType : FolderContentsType . NORMAL ,
45- } ;
46- } else {
47- this . cache . delete ( item . folder_linkId ) ;
48- this . saveMapToStorage ( ) ;
4982 }
83+ this . cache . delete ( item . folder_linkId ) ;
84+ this . saveMapToStorage ( ) ;
5085 }
5186 return {
5287 folderThumb : '' ,
@@ -55,7 +90,15 @@ export class ThumbnailCache {
5590 }
5691
5792 public hasThumbnail ( item : ItemVO ) : boolean {
58- return this . cache . has ( item . folder_linkId ) ;
93+ if ( ! this . cache . has ( item . folder_linkId ) ) {
94+ return false ;
95+ }
96+ if ( isCachedThumbnail ( this . cache . get ( item . folder_linkId ) ) ) {
97+ return true ;
98+ }
99+ this . cache . delete ( item . folder_linkId ) ;
100+ this . saveMapToStorage ( ) ;
101+ return false ;
59102 }
60103
61104 public invalidateFolder ( folderLinkId : number ) : void {
@@ -68,9 +111,9 @@ export class ThumbnailCache {
68111 private fetchCacheMapFromStorage ( ) : void {
69112 const cacheData = this . storage . session . get ( this . STORAGE_KEY ) ;
70113 if ( cacheData && Array . isArray ( cacheData ) ) {
71- this . cache = new Map < number , [ string , string ] > ( cacheData ) ;
114+ this . cache = new Map < number , CachedThumbnail > ( cacheData ) ;
72115 } else {
73- this . cache = new Map < number , [ string , string ] > ( ) ;
116+ this . cache = new Map < number , CachedThumbnail > ( ) ;
74117 this . saveMapToStorage ( ) ;
75118 }
76119 }
0 commit comments