@@ -7,6 +7,38 @@ import * as imageSource from 'tns-core-modules/image-source';
77import * as fs from 'tns-core-modules/file-system' ;
88import { 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+
1042export 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 }
0 commit comments