Skip to content

Commit

Permalink
Fix Fb4aReactPackageTest.testGetNativeModulesByName test
Browse files Browse the repository at this point in the history
Summary:
This diff refactors the ImageLoaderModule class in order to prevent early execution of Fresco.getImagePipeline method when prefetching MC is disabled (see stack D21362266)

changelog: [Internal][Android]

Reviewed By: shergin

Differential Revision: D21368516

fbshipit-source-id: 53f99cd3c3f4848364182cb954a8d34821cb6d9e
  • Loading branch information
mdvacca authored and facebook-github-bot committed May 2, 2020
1 parent eb60019 commit 03385ac
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,21 @@ public class ImageLoaderModule extends NativeImageLoaderAndroidSpec
private static final String ERROR_PREFETCH_FAILURE = "E_PREFETCH_FAILURE";
private static final String ERROR_GET_SIZE_FAILURE = "E_GET_SIZE_FAILURE";
public static final String NAME = "ImageLoader";
private static final Object DEFAULT_CALLER_CONTEXT = new Object();

private @Nullable final Object mCallerContext;
private final Object mEnqueuedRequestMonitor = new Object();
private final SparseArray<DataSource<Void>> mEnqueuedRequests = new SparseArray<>();
private final ImagePipeline mImagePipeline;
private @Nullable ImagePipeline mImagePipeline = null;
private @Nullable ReactCallerContextFactory mCallerContextFactory;

public ImageLoaderModule(ReactApplicationContext reactContext) {
this(reactContext, DEFAULT_CALLER_CONTEXT);
super(reactContext);
mCallerContext = this;
}

public ImageLoaderModule(ReactApplicationContext reactContext, Object callerContext) {
super(reactContext);
mCallerContext = callerContext;
mImagePipeline = Fresco.getImagePipeline();
}

public ImageLoaderModule(
Expand All @@ -84,6 +83,10 @@ public String getName() {
return NAME;
}

private ImagePipeline getImagePipeline() {
return mImagePipeline != null ? mImagePipeline : Fresco.getImagePipeline();
}

/**
* Fetch the width and height of the given image.
*
Expand All @@ -102,7 +105,7 @@ public void getSize(final String uriString, final Promise promise) {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(source.getUri()).build();

DataSource<CloseableReference<CloseableImage>> dataSource =
mImagePipeline.fetchDecodedImage(request, getCallerContext());
getImagePipeline().fetchDecodedImage(request, getCallerContext());

DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
Expand Down Expand Up @@ -163,7 +166,7 @@ public void getSizeWithHeaders(
ReactNetworkImageRequest.fromBuilderWithHeaders(imageRequestBuilder, headers);

DataSource<CloseableReference<CloseableImage>> dataSource =
mImagePipeline.fetchDecodedImage(request, getCallerContext());
getImagePipeline().fetchDecodedImage(request, getCallerContext());

DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
Expand Down Expand Up @@ -223,7 +226,7 @@ public void prefetchImage(
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).build();

DataSource<Void> prefetchSource =
mImagePipeline.prefetchToDiskCache(request, getCallerContext());
getImagePipeline().prefetchToDiskCache(request, getCallerContext());
DataSubscriber<Void> prefetchSubscriber =
new BaseDataSubscriber<Void>() {
@Override
Expand Down Expand Up @@ -270,7 +273,7 @@ public void queryCache(final ReadableArray uris, final Promise promise) {
@Override
protected void doInBackgroundGuarded(Void... params) {
WritableMap result = Arguments.createMap();
ImagePipeline imagePipeline = mImagePipeline;
ImagePipeline imagePipeline = getImagePipeline();
for (int i = 0; i < uris.size(); i++) {
String uriString = uris.getString(i);
final Uri uri = Uri.parse(uriString);
Expand Down

0 comments on commit 03385ac

Please sign in to comment.