Skip to content

Commit 072da6c

Browse files
committed
feat(ImageLoader Provider): re-use FileTransferObject instances
closes #79
1 parent 35c4d39 commit 072da6c

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/components/img-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export class ImgLoader implements OnInit {
178178
this.isLoading = !stopLoading;
179179

180180
if (this._useImg) {
181-
181+
182182
// Using <img> tag
183183
if (!this.element) {
184184
// create img element if we dont have one

src/providers/image-loader.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export class ImageLoader {
5252
*/
5353
private queue: QueueItem[] = [];
5454

55+
private transferInstances: FileTransferObject[] = [];
56+
5557
private processing: number = 0;
5658

5759
private cacheIndex: IndexItem[] = [];
@@ -154,17 +156,6 @@ export class ImageLoader {
154156

155157
}
156158

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-
168159
/**
169160
* Gets the filesystem path of an image.
170161
* 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 {
245236

246237
// take the first item from queue
247238
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];
248249

249250
// process more items concurrently if we can
250251
if (this.canProcess) this.processQueue();
@@ -254,25 +255,26 @@ export class ImageLoader {
254255
// then will execute this function again to process any remaining items
255256
const done = () => {
256257
this.processing--;
258+
this.transferInstances.push(transfer);
257259
this.processQueue();
258260
};
259261

260262
const localPath = this.file.cacheDirectory + this.config.cacheDirectoryName + '/' + this.createFileName(currentItem.imageUrl);
261-
this.downloadImage(currentItem.imageUrl, localPath)
262-
.then((file: FileEntry) => {
263263

264+
transfer.download(currentItem.imageUrl, localPath)
265+
.then((file: FileEntry) => {
264266
if (this.shouldIndex) {
265267
this.addFileToIndex(file).then(this.maintainCacheSize.bind(this));
266268
}
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();
271274
})
272275
.catch((e) => {
273276
currentItem.reject();
274277
this.throwError(e);
275-
276278
done();
277279
});
278280

0 commit comments

Comments
 (0)