Skip to content

Commit

Permalink
feat: prefetching to disk and memory cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ddfreiling committed May 2, 2019
1 parent 3aa255d commit 8cfc296
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/image.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@ import * as imageSource from 'tns-core-modules/image-source';
import * as fs from 'tns-core-modules/file-system';
import { Color } from 'tns-core-modules/color/color';

let BaseDataSubscriber: new (onNewResult: () => void, onFailure: () => void) => com.facebook.datasource.BaseDataSubscriber<any>;

function initializeBaseDataSubscriber() {
if (BaseDataSubscriber) {
return;
}
class BaseDataSubscriberImpl extends com.facebook.datasource.BaseDataSubscriber<any> {
private _onNewResult: () => void;
private _onFailure: () => void;
constructor(onNewResult: () => void, onFailure: () => void) {
super();
this._onNewResult = onNewResult;
this._onFailure = onFailure;
return global.__native(this);
}
public onNewResultImpl(_dataSource: com.facebook.datasource.DataSource<any>): void {
// Store image ref to be released later.
//const mCloseableImageRef = _dataSource.getResult();
if (this._onNewResult) {
this._onNewResult();
}
}

public onFailureImpl(_dataSource: com.facebook.datasource.DataSource<any>): void {
if (this._onFailure) {
this._onFailure();
}
}
};
BaseDataSubscriber = BaseDataSubscriberImpl;
}

export function initialize(config?: ImagePipelineConfigSetting): void {
if (application.android) {
if (config && config.isDownsampleEnabled) {
Expand All @@ -17,6 +49,7 @@ export function initialize(config?: ImagePipelineConfigSetting): void {
} else {
com.facebook.drawee.backends.pipeline.Fresco.initialize(application.android.context);
}
initializeBaseDataSubscriber();
}
}

Expand Down Expand Up @@ -101,6 +134,35 @@ export class ImagePipeline {
this._android.clearDiskCaches();
}

prefetchToDiskCache(uri: string): Promise<void> {
return this.prefetchToCache(uri, true);
}

prefetchToMemoryCache(uri: string): Promise<void> {
return this.prefetchToCache(uri, false);
}

private prefetchToCache(uri: string, toDiskCache: boolean): Promise<void> {
return new Promise((resolve, reject) => {
try {
const nativeUri = android.net.Uri.parse(uri);
const request = com.facebook.imagepipeline.request.ImageRequestBuilder.newBuilderWithSource(nativeUri).build();
let datasource: com.facebook.datasource.DataSource<java.lang.Void>;
if (toDiskCache) {
datasource = this._android.prefetchToDiskCache(request, null);
} else {
datasource = this._android.prefetchToBitmapCache(request, null);
}
datasource.subscribe(
new BaseDataSubscriber(resolve, reject),
com.facebook.common.executors.CallerThreadExecutor.getInstance()
);
} catch (error) {
reject(error);
}
});
}

get android(): any {
return this._android;
}
Expand Down
10 changes: 10 additions & 0 deletions src/image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ export class ImagePipeline {
* Clear disk caches.
*/
clearDiskCaches(): void;

/**
* Prefetch to disk cache.
*/
prefetchToDiskCache(uri: string): Promise<void>;

/**
* Prefetch to memory cache.
*/
prefetchToMemoryCache(uri: string): Promise<void>;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/image.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,30 @@ export class ImagePipeline {
clearDiskCaches() {
this._ios.clearDiskOnCompletion(null);
}

prefetchToDiskCache(uri: string): Promise<void> {
return this.prefetchToCacheType(uri, SDImageCacheType.Disk);
}

prefetchToMemoryCache(uri: string): Promise<void> {
return this.prefetchToCacheType(uri, SDImageCacheType.Memory);
}

private prefetchToCacheType(uri: string, cacheType: SDImageCacheType): Promise<void> {
return new Promise((resolve, reject) => {
const context = NSMutableDictionary.alloc<string, any>().initWithCapacity(1);
context.setObjectForKey(cacheType, SDWebImageContextStoreCacheType);
SDWebImagePrefetcher.sharedImagePrefetcher.context = context;
SDWebImagePrefetcher.sharedImagePrefetcher.prefetchURLsProgressCompleted([getUri(uri)], null, (finished, skipped) => {
if (finished && !skipped) {
resolve();
} else {
reject(`prefetch failed for URI: ${uri}`);
}
});
});
}

get ios() {
return this._ios;
}
Expand Down

0 comments on commit 8cfc296

Please sign in to comment.