Skip to content

Added image pre-fetching #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,20 @@ Default cache purge time can be specified in number of days.


**for android, you need to initialize in the application onlaunch event before clearing the cache**

### Prefetching images

It is possible to prefetch images so that they are already loaded in the cache before use.

```
let urls = [
url1,
url2
]

preFetchImage(urls)
.then(() => { console.log('prefetch success!'); })
.catch((error) => { console.error('prefetch error: '+error); });
```

** note: on iOS calling preFetchImage multiple times before the promise resolves will cancel previous calls
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ export function initializeOnAngular(): void;
* @param days : limit of the cache
*/
export function setCacheLimit(days: number): void;

/**
* To prefetch images to allow loading before using the images
* @param days : array of URLs to prefetch
*/
export function preFetchImage(urls:Array<string>) : Promise<void>;
2 changes: 1 addition & 1 deletion ios-ts-lib/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ios-ts-lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Helpers {
public static getPlaceholderUIImage(value) {
if (types.isString(value)) {
if (utils.isFileOrResourcePath(value)) {
return imageSource.fromFileOrResource(value).ios;
return imageSource.ImageSource.fromFileOrResourceSync(value).ios;
}
}

Expand Down
28 changes: 3 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
{
"_from": "nativescript-web-image-cache",
"_id": "[email protected]",
"_inBundle": false,
"_integrity": "sha512-zIFzI09vBuQcODCdxuDgtBWmtTxNSxm06win+IU7t2zblk2ItdGawDbwd0ZLX9RN+moUf/ytTj+MMoBgowdt6Q==",
"_location": "/nativescript-web-image-cache",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "nativescript-web-image-cache",
"name": "nativescript-web-image-cache",
"escapedName": "nativescript-web-image-cache",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/nativescript-web-image-cache/-/nativescript-web-image-cache-4.2.4.tgz",
"_shasum": "acc14c6684fc2dd7481a66f1f01a98b00e1ce62d",
"_spec": "nativescript-web-image-cache",
"_where": "/Users/Sumeet/smg/gudds/NKY/ns/sample",
"author": {
"name": "Batheja Sumeet"
},
Expand Down Expand Up @@ -61,7 +37,9 @@
"type": "git",
"url": "git+https://github.com/VideoSpike/nativescript-web-image-cache.git"
},
"scripts": {},
"scripts": {
"build": "./node_modules/.bin/tsc"
},
"typings": "web-image-cache.d.ts",
"version": "5.0.0"
}
8 changes: 2 additions & 6 deletions platforms/android/include.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* Include.gradle configuration: http://docs.nativescript.org/plugins/plugins#includegradle-specification */

android {
productFlavors {
"nativescript-web-image-cache" {
dimension "nativescript-web-image-cache"
}
}

}

dependencies {
compile "com.facebook.fresco:fresco:1.10.0"
compile "com.facebook.fresco:fresco:2.3.0"
}
2 changes: 1 addition & 1 deletion platforms/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pod 'SDWebImage', '~> 4.4.1'
pod 'SDWebImage', '~> 5.9.4'
12 changes: 11 additions & 1 deletion types/fresco.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
declare namespace com {
export namespace facebook {
export namespace datasource {
export class BaseDataSubscriber {
public static extend(param: any);
}
export class DataSource { }
}
export namespace common {
export namespace references {
export class CloseableReference { }
}
export namespace executors {
export class UiThreadImmediateExecutorService {
public static getInstance() : any;
}
}
export namespace internal {
export class Supplier { }
}
Expand All @@ -18,7 +26,9 @@ declare namespace com {
export class ImagePipeline { }
}
export namespace request {
export class ImageRequest { }
export class ImageRequest {
public static fromUri(param0: any): any;
}
}
export namespace image {
export class ImageInfo { }
Expand Down
1 change: 1 addition & 0 deletions web-image-cache.android.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export declare function setCacheLimit(numberOfDays: any): void;
export declare function initialize(): void;
export declare function clearCache(): void;
export declare function initializeOnAngular(): void;
export declare function preFetchImage(urls: Array<string>): Promise<void>;
32 changes: 31 additions & 1 deletion web-image-cache.android.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web-image-cache.android.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions web-image-cache.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,43 @@ export function clearCache() {
export function initializeOnAngular() {
throw new Error("'initializeOnAngular' has been removed from 'nativescript-web-image-cache', see its readme for details!");
}

export function preFetchImage(urls: Array<string>) : Promise<void> {
return new Promise<void>((resolve, reject) => {
if (!urls || !Array.isArray(urls) || urls.length<1) {
reject(`preFetchImage: param should be array of urls`);
} else {
let counter:number=0;

urls.forEach((url) => {
const uri = android.net.Uri.parse(url);
const prefetchSubscriber = com.facebook.datasource.BaseDataSubscriber.extend({
onNewResultImpl(dataSource) {
counter++;
if (counter===urls.length) { resolve(); }
},
onFailureImpl(dataSource) {
counter++;
if (counter===urls.length) {
reject(`preFetchImage: failed to prefetch ${uri.toString()}`);
}
}
});

/*
prefetchToDiskCache() --> load to disk slower but less CPU
prefetchToBitmapCache --> load to memory cache, faster but more CPU
*/
let dataSource = com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline().prefetchToBitmapCache(
com.facebook.imagepipeline.request.ImageRequest.fromUri(uri),
application.android.context
);

dataSource.subscribe(
new prefetchSubscriber(),
com.facebook.common.executors.UiThreadImmediateExecutorService.getInstance()
);
});
}
});
}
1 change: 1 addition & 0 deletions web-image-cache.ios.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export declare class WebImage extends WebImageCommon {
export declare function setCacheLimit(numberOfDays: any): void;
export declare function clearCache(): void;
export declare function initializeOnAngular(): void;
export declare function preFetchImage(urls: Array<string>): Promise<void>;
Loading