Skip to content

Commit 8cfc296

Browse files
committed
feat: prefetching to disk and memory cache
1 parent 3aa255d commit 8cfc296

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/image.android.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ import * as imageSource from 'tns-core-modules/image-source';
77
import * as fs from 'tns-core-modules/file-system';
88
import { Color } from 'tns-core-modules/color/color';
99

10+
let BaseDataSubscriber: new (onNewResult: () => void, onFailure: () => void) => com.facebook.datasource.BaseDataSubscriber<any>;
11+
12+
function initializeBaseDataSubscriber() {
13+
if (BaseDataSubscriber) {
14+
return;
15+
}
16+
class BaseDataSubscriberImpl extends com.facebook.datasource.BaseDataSubscriber<any> {
17+
private _onNewResult: () => void;
18+
private _onFailure: () => void;
19+
constructor(onNewResult: () => void, onFailure: () => void) {
20+
super();
21+
this._onNewResult = onNewResult;
22+
this._onFailure = onFailure;
23+
return global.__native(this);
24+
}
25+
public onNewResultImpl(_dataSource: com.facebook.datasource.DataSource<any>): void {
26+
// Store image ref to be released later.
27+
//const mCloseableImageRef = _dataSource.getResult();
28+
if (this._onNewResult) {
29+
this._onNewResult();
30+
}
31+
}
32+
33+
public onFailureImpl(_dataSource: com.facebook.datasource.DataSource<any>): void {
34+
if (this._onFailure) {
35+
this._onFailure();
36+
}
37+
}
38+
};
39+
BaseDataSubscriber = BaseDataSubscriberImpl;
40+
}
41+
1042
export function initialize(config?: ImagePipelineConfigSetting): void {
1143
if (application.android) {
1244
if (config && config.isDownsampleEnabled) {
@@ -17,6 +49,7 @@ export function initialize(config?: ImagePipelineConfigSetting): void {
1749
} else {
1850
com.facebook.drawee.backends.pipeline.Fresco.initialize(application.android.context);
1951
}
52+
initializeBaseDataSubscriber();
2053
}
2154
}
2255

@@ -101,6 +134,35 @@ export class ImagePipeline {
101134
this._android.clearDiskCaches();
102135
}
103136

137+
prefetchToDiskCache(uri: string): Promise<void> {
138+
return this.prefetchToCache(uri, true);
139+
}
140+
141+
prefetchToMemoryCache(uri: string): Promise<void> {
142+
return this.prefetchToCache(uri, false);
143+
}
144+
145+
private prefetchToCache(uri: string, toDiskCache: boolean): Promise<void> {
146+
return new Promise((resolve, reject) => {
147+
try {
148+
const nativeUri = android.net.Uri.parse(uri);
149+
const request = com.facebook.imagepipeline.request.ImageRequestBuilder.newBuilderWithSource(nativeUri).build();
150+
let datasource: com.facebook.datasource.DataSource<java.lang.Void>;
151+
if (toDiskCache) {
152+
datasource = this._android.prefetchToDiskCache(request, null);
153+
} else {
154+
datasource = this._android.prefetchToBitmapCache(request, null);
155+
}
156+
datasource.subscribe(
157+
new BaseDataSubscriber(resolve, reject),
158+
com.facebook.common.executors.CallerThreadExecutor.getInstance()
159+
);
160+
} catch (error) {
161+
reject(error);
162+
}
163+
});
164+
}
165+
104166
get android(): any {
105167
return this._android;
106168
}

src/image.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,16 @@ export class ImagePipeline {
387387
* Clear disk caches.
388388
*/
389389
clearDiskCaches(): void;
390+
391+
/**
392+
* Prefetch to disk cache.
393+
*/
394+
prefetchToDiskCache(uri: string): Promise<void>;
395+
396+
/**
397+
* Prefetch to memory cache.
398+
*/
399+
prefetchToMemoryCache(uri: string): Promise<void>;
390400
}
391401

392402
/**

src/image.ios.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,30 @@ export class ImagePipeline {
200200
clearDiskCaches() {
201201
this._ios.clearDiskOnCompletion(null);
202202
}
203+
204+
prefetchToDiskCache(uri: string): Promise<void> {
205+
return this.prefetchToCacheType(uri, SDImageCacheType.Disk);
206+
}
207+
208+
prefetchToMemoryCache(uri: string): Promise<void> {
209+
return this.prefetchToCacheType(uri, SDImageCacheType.Memory);
210+
}
211+
212+
private prefetchToCacheType(uri: string, cacheType: SDImageCacheType): Promise<void> {
213+
return new Promise((resolve, reject) => {
214+
const context = NSMutableDictionary.alloc<string, any>().initWithCapacity(1);
215+
context.setObjectForKey(cacheType, SDWebImageContextStoreCacheType);
216+
SDWebImagePrefetcher.sharedImagePrefetcher.context = context;
217+
SDWebImagePrefetcher.sharedImagePrefetcher.prefetchURLsProgressCompleted([getUri(uri)], null, (finished, skipped) => {
218+
if (finished && !skipped) {
219+
resolve();
220+
} else {
221+
reject(`prefetch failed for URI: ${uri}`);
222+
}
223+
});
224+
});
225+
}
226+
203227
get ios() {
204228
return this._ios;
205229
}

0 commit comments

Comments
 (0)