Skip to content

Commit

Permalink
AssetSourceResolver: use resolver defined by PackagerAsset (#45897)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #45897

Changelog:
[General][Added] - Use PackagerAsset resolver instead of Platform.OS when its provided.

Reviewed By: christophpurrer

Differential Revision: D60670503

fbshipit-source-id: 9732b0ee3ec0cd1424bd83a4cc18924bcbd88db5
  • Loading branch information
andrewdacenko authored and facebook-github-bot committed Aug 7, 2024
1 parent 3f3a85b commit baa3666
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/react-native/Libraries/Image/AssetSourceResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export type ResolvedAssetSource = {|
+scale: number,
|};

import type {PackagerAsset} from '@react-native/assets-registry/registry';
import type {
AssetDestPathResolver,
PackagerAsset,
} from '@react-native/assets-registry/registry';

const PixelRatio = require('../Utilities/PixelRatio').default;
const Platform = require('../Utilities/Platform');
Expand Down Expand Up @@ -76,6 +79,10 @@ class AssetSourceResolver {
return this.assetServerURL();
}

if (this.asset.resolver != null) {
return this.getAssetUsingResolver(this.asset.resolver);
}

if (Platform.OS === 'android') {
return this.isLoadedFromFileSystem()
? this.drawableFolderInBundle()
Expand All @@ -85,6 +92,26 @@ class AssetSourceResolver {
}
}

getAssetUsingResolver(resolver: AssetDestPathResolver): ResolvedAssetSource {
switch (resolver) {
case 'android':
return this.isLoadedFromFileSystem()
? this.drawableFolderInBundle()
: this.resourceIdentifierWithoutScale();
case 'generic':
return this.scaledAssetURLNearBundle();
default:
throw new Error(
"Don't know how to get asset via provided resolver: " +
resolver +
'\nAsset: ' +
JSON.stringify(this.asset, null, '\t') +
'\nPossible resolvers are:' +
JSON.stringify(['android', 'generic'], null, '\t'),
);
}
}

/**
* Returns an absolute URL which can be used to fetch the asset
* from the devserver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4679,6 +4679,7 @@ declare class AssetSourceResolver {
isLoadedFromServer(): boolean;
isLoadedFromFileSystem(): boolean;
defaultAsset(): ResolvedAssetSource;
getAssetUsingResolver(resolver: AssetDestPathResolver): ResolvedAssetSource;
assetServerURL(): ResolvedAssetSource;
scaledAssetPath(): ResolvedAssetSource;
scaledAssetURLNearBundle(): ResolvedAssetSource;
Expand Down

0 comments on commit baa3666

Please sign in to comment.