Skip to content

Commit

Permalink
Log prefetched image's view controller moduleName and GraphQL queryRo…
Browse files Browse the repository at this point in the history
…otName

Reviewed By: fkgozali, rubennorte

Differential Revision: D24683194

fbshipit-source-id: 5a3f37123c0d9f6f40124e131212e6bf190193cc
  • Loading branch information
p-sun authored and facebook-github-bot committed Nov 7, 2020
1 parent 00cfb0f commit 017bc91
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
22 changes: 22 additions & 0 deletions Libraries/Image/Image.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ function getSizeWithHeaders(
);
}

function prefetchWithMetadata(
url: string,
queryRootName: string,
rootTag?: ?number,
callback: ?Function,
): any {
// TODO: T79192300 Log queryRootName and rootTag
prefetch(url, callback);
}

function prefetch(url: string, callback: ?Function): any {
const requestId = generateRequestId();
callback && callback(requestId);
Expand All @@ -219,6 +229,7 @@ type ImageComponentStatics = $ReadOnly<{|
getSize: typeof getSize,
getSizeWithHeaders: typeof getSizeWithHeaders,
prefetch: typeof prefetch,
prefetchWithMetadata: typeof prefetchWithMetadata,
abortPrefetch: typeof abortPrefetch,
queryCache: typeof queryCache,
resolveAssetSource: typeof resolveAssetSource,
Expand Down Expand Up @@ -358,6 +369,17 @@ Image.getSizeWithHeaders = getSizeWithHeaders;
* comment and run Flow. */
Image.prefetch = prefetch;

/**
* Prefetches a remote image for later use by downloading it to the disk
* cache, and adds metadata for queryRootName and rootTag.
*
* See https://reactnative.dev/docs/image.html#prefetch
*/
/* $FlowFixMe(>=0.89.0 site=react_native_android_fb) This comment suppresses an
* error found when Flow v0.89 was deployed. To see the error, delete this
* comment and run Flow. */
Image.prefetchWithMetadata = prefetchWithMetadata;

/**
* Abort prefetch request.
*
Expand Down
28 changes: 28 additions & 0 deletions Libraries/Image/Image.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ function getSizeWithHeaders(
);
}

function prefetchWithMetadata(
url: string,
queryRootName: string,
rootTag?: ?number,
): any {
if (NativeImageLoaderIOS.prefetchImageWithMetadata) {
return NativeImageLoaderIOS.prefetchImageWithMetadata(
url,
queryRootName,
rootTag,
);
} else {
return NativeImageLoaderIOS.prefetchImage(url);
}
}

function prefetch(url: string): any {
return NativeImageLoaderIOS.prefetchImage(url);
}
Expand All @@ -73,6 +89,7 @@ type ImageComponentStatics = $ReadOnly<{|
getSize: typeof getSize,
getSizeWithHeaders: typeof getSizeWithHeaders,
prefetch: typeof prefetch,
prefetchWithMetadata: typeof prefetchWithMetadata,
queryCache: typeof queryCache,
resolveAssetSource: typeof resolveAssetSource,
propTypes: typeof DeprecatedImagePropType,
Expand Down Expand Up @@ -181,6 +198,17 @@ Image.getSizeWithHeaders = getSizeWithHeaders;
* comment and run Flow. */
Image.prefetch = prefetch;

/**
* Prefetches a remote image for later use by downloading it to the disk
* cache, and adds metadata for queryRootName and rootTag.
*
* See https://reactnative.dev/docs/image.html#prefetch
*/
/* $FlowFixMe(>=0.89.0 site=react_native_ios_fb) This comment suppresses an
* error found when Flow v0.89 was deployed. To see the error, delete this
* comment and run Flow. */
Image.prefetchWithMetadata = prefetchWithMetadata;

/**
* Performs cache interrogation.
*
Expand Down
5 changes: 5 additions & 0 deletions Libraries/Image/NativeImageLoaderIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export interface Spec extends TurboModule {
...
}>;
+prefetchImage: (uri: string) => Promise<boolean>;
+prefetchImageWithMetadata?: (
uri: string,
queryRootName: string,
rootTag?: ?number,
) => Promise<boolean>;
+queryCache: (uris: Array<string>) => Promise<Object>;
}

Expand Down
25 changes: 22 additions & 3 deletions Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ - (void)setImageCache:(id<RCTImageCache>)cache
if (!_loaders) {
std::unique_lock<std::mutex> guard(_loadersMutex);
if (!_loaders) {

// Get loaders, sorted in reverse priority order (highest priority first)
if (_loadersProvider) {
_loaders = _loadersProvider();
Expand Down Expand Up @@ -1196,11 +1196,30 @@ - (void)cancelRequest:(id)requestToken
RCT_EXPORT_METHOD(prefetchImage:(NSString *)uri
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
[self prefetchImageWithMetadata:uri queryRootName:nil rootTag:nil resolve:resolve reject:reject];
}

RCT_EXPORT_METHOD(prefetchImageWithMetadata:(NSString *)uri
queryRootName:(NSString *)queryRootName
rootTag:(NSNumber *)rootTag
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
NSURLRequest *request = [RCTConvert NSURLRequest:uri];
[self loadImageWithURLRequest:request
priority:RCTImageLoaderPriorityPrefetch
callback:^(NSError *error, UIImage *image) {
size:CGSizeZero
scale:1
clipped:YES
resizeMode:RCTResizeModeStretch
priority:RCTImageLoaderPriorityPrefetch
attribution:{
.queryRootName = queryRootName ? [queryRootName UTF8String] : "",
.surfaceId = [rootTag intValue],
}
progressBlock:nil
partialLoadBlock:nil
completionBlock:^(NSError *error, UIImage *image, id completionMetadata) {
if (error) {
reject(@"E_PREFETCH_FAILURE", nil, error);
return;
Expand Down
1 change: 1 addition & 0 deletions Libraries/Image/RCTImageURLLoaderWithAttribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace react {
struct ImageURLLoaderAttribution {
int32_t nativeViewTag = 0;
int32_t surfaceId = 0;
std::string queryRootName;
NSString *analyticTag;
};

Expand Down

0 comments on commit 017bc91

Please sign in to comment.