@@ -7,6 +7,38 @@ import * as imageSource from 'tns-core-modules/image-source';
7
7
import * as fs from 'tns-core-modules/file-system' ;
8
8
import { Color } from 'tns-core-modules/color/color' ;
9
9
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
+
10
42
export function initialize ( config ?: ImagePipelineConfigSetting ) : void {
11
43
if ( application . android ) {
12
44
if ( config && config . isDownsampleEnabled ) {
@@ -17,6 +49,7 @@ export function initialize(config?: ImagePipelineConfigSetting): void {
17
49
} else {
18
50
com . facebook . drawee . backends . pipeline . Fresco . initialize ( application . android . context ) ;
19
51
}
52
+ initializeBaseDataSubscriber ( ) ;
20
53
}
21
54
}
22
55
@@ -101,6 +134,35 @@ export class ImagePipeline {
101
134
this . _android . clearDiskCaches ( ) ;
102
135
}
103
136
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
+
104
166
get android ( ) : any {
105
167
return this . _android ;
106
168
}
0 commit comments