@@ -52,6 +52,8 @@ export class ImageLoader {
52
52
*/
53
53
private queue : QueueItem [ ] = [ ] ;
54
54
55
+ private transferInstances : FileTransferObject [ ] = [ ] ;
56
+
55
57
private processing : number = 0 ;
56
58
57
59
private cacheIndex : IndexItem [ ] = [ ] ;
@@ -154,17 +156,6 @@ export class ImageLoader {
154
156
155
157
}
156
158
157
- /**
158
- * Downloads an image via cordova-plugin-file-transfer
159
- * @param imageUrl {string} The remote URL of the image
160
- * @param localPath {string} The local path to store the image at
161
- * @returns {Promise<any> } Returns a promise that resolves when the download is complete, or rejects on error.
162
- */
163
- private downloadImage ( imageUrl : string , localPath : string ) : Promise < any > {
164
- const transfer : FileTransferObject = this . fileTransfer . create ( ) ;
165
- return transfer . download ( imageUrl , localPath , true ) ;
166
- }
167
-
168
159
/**
169
160
* Gets the filesystem path of an image.
170
161
* This will return the remote path if anything goes wrong or if the cache service isn't ready yet.
@@ -245,6 +236,16 @@ export class ImageLoader {
245
236
246
237
// take the first item from queue
247
238
const currentItem : QueueItem = this . queue . splice ( 0 , 1 ) [ 0 ] ;
239
+
240
+ // create FileTransferObject instance if needed
241
+ // we would only reach here if current jobs < concurrency limit
242
+ // so, there's no need to check anything other than the length of
243
+ // the FileTransferObject instances we have in memory
244
+ if ( this . transferInstances . length === 0 ) {
245
+ this . transferInstances . push ( this . fileTransfer . create ( ) ) ;
246
+ }
247
+
248
+ const transfer : FileTransferObject = this . transferInstances . splice ( 0 , 1 ) [ 0 ] ;
248
249
249
250
// process more items concurrently if we can
250
251
if ( this . canProcess ) this . processQueue ( ) ;
@@ -254,25 +255,26 @@ export class ImageLoader {
254
255
// then will execute this function again to process any remaining items
255
256
const done = ( ) => {
256
257
this . processing -- ;
258
+ this . transferInstances . push ( transfer ) ;
257
259
this . processQueue ( ) ;
258
260
} ;
259
261
260
262
const localPath = this . file . cacheDirectory + this . config . cacheDirectoryName + '/' + this . createFileName ( currentItem . imageUrl ) ;
261
- this . downloadImage ( currentItem . imageUrl , localPath )
262
- . then ( ( file : FileEntry ) => {
263
263
264
+ transfer . download ( currentItem . imageUrl , localPath )
265
+ . then ( ( file : FileEntry ) => {
264
266
if ( this . shouldIndex ) {
265
267
this . addFileToIndex ( file ) . then ( this . maintainCacheSize . bind ( this ) ) ;
266
268
}
267
- this . getCachedImagePath ( currentItem . imageUrl ) . then ( ( localUrl ) => {
268
- currentItem . resolve ( localUrl ) ;
269
- done ( ) ;
270
- } ) ;
269
+ return this . getCachedImagePath ( currentItem . imageUrl ) ;
270
+ } )
271
+ . then ( ( localUrl ) => {
272
+ currentItem . resolve ( localUrl ) ;
273
+ done ( ) ;
271
274
} )
272
275
. catch ( ( e ) => {
273
276
currentItem . reject ( ) ;
274
277
this . throwError ( e ) ;
275
-
276
278
done ( ) ;
277
279
} ) ;
278
280
0 commit comments