@@ -16,8 +16,6 @@ catch (error)
1616 zstd = zstdEncoder = zstdDecoder = false ;
1717}
1818
19- var queuedImages = [ ] , processingTheImageQueue = false ;
20-
2119var cacheFolder = p . join ( electronRemote . app . getPath ( 'cache' ) , 'opencomic' ) ;
2220
2321if ( process . platform == 'win32' || process . platform == 'win64' )
@@ -27,13 +25,8 @@ if(!fs.existsSync(cacheFolder)) fs.mkdirSync(cacheFolder);
2725cacheFolder = p . join ( cacheFolder , 'cache' ) ;
2826if ( ! fs . existsSync ( cacheFolder ) ) fs . mkdirSync ( cacheFolder ) ;
2927
30- var imagesWithoutSaving = 0 ;
31-
32- async function processTheImageQueue ( )
28+ async function processTheImageQueue ( img = false )
3329{
34- let img = queuedImages . shift ( ) ;
35- if ( ! img ) return ;
36-
3730 let sha = img . sha ;
3831
3932 let realPath = fileManager . realPath ( img . file ) ;
@@ -51,65 +44,31 @@ async function processTheImageQueue()
5144 if ( inArray ( fileExtension ( realPath ) , imageExtensions . blob ) ) // Convert unsupported images to blob
5245 blob = await workers . convertImageToBlob ( realPath ) ;
5346
54- image . resize ( blob || realPath , toImage , { blob : blob ? true : false , width : img . size , height : Math . round ( img . size * ( img . type ? ratios [ img . type ] [ forceSize ] : 1.5 ) ) , quality : 95 , fit : img . type ? fit [ img . type ] : 'inside' } ) . then ( function ( ) {
47+ return new Promise ( function ( resolve , reject ) {
5548
56- //if (blob) fileManager.revokeObjectURL(path);
49+ image . resize ( blob || realPath , toImage , { blob : blob ? true : false , width : img . size , height : Math . round ( img . size * ( img . type ? ratios [ img . type ] [ forceSize ] : 1.5 ) ) , quality : 95 , fit : img . type ? fit [ img . type ] : 'inside' } ) . then ( function ( ) {
5750
58- if ( typeof data [ sha ] == 'undefined' ) data [ sha ] = { lastAccess : app . time ( ) } ;
51+ // if(blob) fileManager.revokeObjectURL(path) ;
5952
60- data [ sha ] . size = img . size ;
53+ if ( typeof data [ sha ] == 'undefined' ) data [ sha ] = { lastAccess : app . time ( ) } ;
6154
62- img . callback ( { cache : true , path : escapeBackSlash ( addCacheVars ( toImage , img . size , img . sha ) ) , sha : sha } , img . vars ) ;
55+ data [ sha ] . size = img . size ;
6356
64- if ( queuedImages . length > 0 )
65- {
66- imagesWithoutSaving ++ ;
57+ img . callback ( { cache : true , path : escapeBackSlash ( addCacheVars ( toImage , img . size , img . sha ) ) , sha : sha } , img . vars ) ;
6758
68- process . nextTick ( function ( ) {
69- processTheImageQueue ( ) ;
70- } ) ;
59+ checkImagesWithoutSaving ( ) ;
60+ resolve ( ) ;
7161
72- if ( imagesWithoutSaving > 50 )
73- {
74- imagesWithoutSaving = 0 ;
75- storage . setThrottle ( 'cache' , data ) ;
76- }
77- }
78- else
79- {
80- imagesWithoutSaving = 0 ;
81- processingTheImageQueue = false ;
62+ } ) . catch ( function ( ) {
8263
83- storage . setThrottle ( 'cache' , data ) ;
84- }
64+ //if(blob) fileManager.revokeObjectURL(path);
8565
86- } ) . catch ( function ( ) {
66+ img . callback ( { cache : true , path : escapeBackSlash ( realPath ) , sha : sha } , img . vars ) ;
8767
88- //if(blob) fileManager.revokeObjectURL(path);
89-
90- img . callback ( { cache : true , path : escapeBackSlash ( realPath ) , sha : sha } , img . vars ) ;
91-
92- if ( queuedImages . length > 0 )
93- {
94- imagesWithoutSaving ++ ;
95-
96- process . nextTick ( function ( ) {
97- processTheImageQueue ( ) ;
98- } ) ;
99-
100- if ( imagesWithoutSaving > 50 )
101- {
102- imagesWithoutSaving = 0 ;
103- storage . setThrottle ( 'cache' , data ) ;
104- }
105- }
106- else
107- {
108- imagesWithoutSaving = 0 ;
109- processingTheImageQueue = false ;
68+ checkImagesWithoutSaving ( ) ;
69+ resolve ( ) ;
11070
111- storage . setThrottle ( 'cache' , data ) ;
112- }
71+ } ) ;
11372
11473 } ) ;
11574}
@@ -121,44 +80,42 @@ async function addImageToQueue(file, size, sha, callback, vars, type, forceSize)
12180 if ( inArray ( extention , imageExtensions . convert ) ) // Convert unsupported images
12281 await workers . convertImage ( file ) ;
12382
124- queuedImages . push ( { file : file , size : size , sha : sha , callback : callback , vars : vars , type : type , forceSize : forceSize } ) ;
125-
126- if ( ! processingTheImageQueue && ! stopTheImageQueue )
127- {
128- processingTheImageQueue = true ;
83+ threads . job ( 'cache' , { useThreads : 0.49 } , processTheImageQueue , { file : file , size : size , sha : sha , callback : callback , vars : vars , type : type , forceSize : forceSize } ) ;
12984
130- setTimeout ( function ( ) {
85+ threads . end ( 'cache' , function ( ) {
13186
132- process . nextTick ( function ( ) {
133- processTheImageQueue ( ) ;
134- } ) ;
87+ imagesWithoutSaving = 0 ;
88+ storage . setThrottle ( 'cache' , data ) ;
13589
136- } , 0 ) ;
137- }
90+ } ) ;
13891}
13992
140- var stopTheImageQueue = false ;
141-
14293function stopQueue ( )
14394{
144- stopTheImageQueue = true ;
95+ threads . stop ( 'cache' ) ;
14596}
14697
14798function resumeQueue ( )
14899{
149- stopTheImageQueue = false ;
100+ threads . resume ( 'cache' ) ;
101+ }
150102
151- if ( ! processingTheImageQueue && queuedImages . length > 0 )
152- {
153- processingTheImageQueue = true ;
103+ function cleanQueue ( )
104+ {
105+ threads . clean ( 'cache' ) ;
106+ if ( data !== false ) storage . setThrottle ( 'cache' , data ) ;
107+ }
154108
155- setTimeout ( function ( ) {
109+ var imagesWithoutSaving = 0 ;
156110
157- process . nextTick ( function ( ) {
158- processTheImageQueue ( ) ;
159- } ) ;
111+ function checkImagesWithoutSaving ( )
112+ {
113+ imagesWithoutSaving ++ ;
160114
161- } , 0 ) ;
115+ if ( imagesWithoutSaving > 50 )
116+ {
117+ imagesWithoutSaving = 0 ;
118+ storage . setThrottle ( 'cache' , data ) ;
162119 }
163120}
164121
@@ -235,12 +192,6 @@ function addImageVars(image)
235192 return image . path + ( vars . length ? '?' + vars . join ( '&' ) : '' ) ;
236193}
237194
238- function cleanQueue ( )
239- {
240- queuedImages . splice ( 1 , queuedImages . length - 1 ) ;
241- if ( data !== false ) storage . setThrottle ( 'cache' , data ) ;
242- }
243-
244195var data = false ;
245196
246197function returnThumbnailsImages ( images , callback , file = false )
@@ -890,8 +841,6 @@ module.exports = {
890841 imageSizeSha : imageSizeSha ,
891842 addImageVars : addImageVars ,
892843 jsonMemory : function ( ) { return jsonMemory } ,
893- queuedImages : function ( ) { return queuedImages } ,
894- processingTheImageQueue : function ( ) { return processingTheImageQueue } ,
895844 stopQueue : stopQueue ,
896845 resumeQueue : resumeQueue ,
897846 purge : purge ,
0 commit comments