Skip to content

Commit a633c67

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Move AssetRegistry implementation into main package
Summary: Resolves T275935135. Differential Revision: D108750302
1 parent 67381e1 commit a633c67

14 files changed

Lines changed: 121 additions & 63 deletions

File tree

packages/assets-registry/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Most apps never import this directly — assets are handled through `<Image>`.
1313

1414
### `@react-native/assets-registry/registry`
1515

16+
> [!Note]
17+
> Aliases to [`AssetRegistry`](https://reactnative.dev/docs/assetregistry) (since 0.87). Prefer importing directly from the `'react-native'` package in libraries.
18+
1619
| Export | Signature | Notes |
1720
|---|---|---|
1821
| `registerAsset` | `(asset: PackagerAsset) => number` | Stores the asset; returns a numeric ID |

packages/assets-registry/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
"!**/__fixtures__/**",
2727
"!**/__mocks__/**",
2828
"!**/__tests__/**"
29-
]
29+
],
30+
"peerDependencies": {
31+
"react-native": "*"
32+
}
3033
}

packages/assets-registry/path-support.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
*/
1010

packages/assets-registry/registry.js

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,20 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
*/
1010

1111
'use strict';
1212

13-
/*::
14-
export type AssetDestPathResolver = 'android' | 'generic';
13+
import {AssetRegistry} from 'react-native';
1514

16-
export type PackagerAsset = {
17-
readonly __packager_asset: boolean,
18-
readonly fileSystemLocation: string,
19-
readonly httpServerLocation: string,
20-
readonly width: ?number,
21-
readonly height: ?number,
22-
readonly scales: Array<number>,
23-
readonly hash: string,
24-
readonly name: string,
25-
readonly type: string,
26-
readonly resolver?: AssetDestPathResolver,
27-
...
28-
};
15+
/*::
16+
export type {AssetDestPathResolver, PackagerAsset} from 'react-native';
2917
*/
3018

31-
const assets /*: Array<PackagerAsset> */ = [];
32-
33-
function registerAsset(asset /*: PackagerAsset */) /*: number */ {
34-
// `push` returns new array length, so the first asset will
35-
// get id 1 (not 0) to make the value truthy
36-
return assets.push(asset);
37-
}
38-
39-
function getAssetByID(assetId /*: number */) /*: PackagerAsset */ {
40-
return assets[assetId - 1];
41-
}
42-
4319
// eslint-disable-next-line @react-native/monorepo/no-commonjs-exports
44-
module.exports = {registerAsset, getAssetByID};
20+
module.exports = {
21+
registerAsset: AssetRegistry.registerAsset,
22+
getAssetByID: AssetRegistry.getAssetByID,
23+
};

packages/react-native/Libraries/Image/AssetRegistry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
*/
1010

packages/react-native/Libraries/Image/AssetSourceResolver.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,10 @@
1010

1111
'use strict';
1212

13-
export type ResolvedAssetSource = {
14-
readonly __packager_asset: boolean,
15-
readonly width: ?number,
16-
readonly height: ?number,
17-
readonly uri: string,
18-
readonly scale: number,
19-
};
20-
21-
// From @react-native/assets-registry
22-
type AssetDestPathResolver = 'android' | 'generic';
23-
24-
// From @react-native/assets-registry
25-
type PackagerAsset = Readonly<{
26-
__packager_asset: boolean,
27-
fileSystemLocation: string,
28-
httpServerLocation: string,
29-
width: ?number,
30-
height: ?number,
31-
scales: Array<number>,
32-
hash: string,
33-
name: string,
34-
type: string,
35-
resolver?: AssetDestPathResolver,
36-
...
37-
}>;
13+
import type {
14+
AssetDestPathResolver,
15+
PackagerAsset,
16+
} from '../../src/private/assets/AssetRegistry';
3817

3918
const PixelRatio = require('../Utilities/PixelRatio').default;
4019
const Platform = require('../Utilities/Platform').default;
@@ -46,6 +25,14 @@ const {
4625
} = require('@react-native/assets-registry/path-support');
4726
const invariant = require('invariant');
4827

28+
export type ResolvedAssetSource = {
29+
readonly __packager_asset: boolean,
30+
readonly width: ?number,
31+
readonly height: ?number,
32+
readonly uri: string,
33+
readonly scale: number,
34+
};
35+
4936
/**
5037
* Returns a path like 'assets/AwesomeModule/icon@2x.png'
5138
*/

packages/react-native/Libraries/Image/RelativeImageStub.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// This is a stub for flow to make it understand require('./icon.png')
1414
// See metro/src/Bundler/index.js
1515

16-
const AssetRegistry = require('@react-native/assets-registry/registry');
16+
const {AssetRegistry} = require('../../src/private/assets/AssetRegistry');
1717

1818
const RelativeImageStub = AssetRegistry.registerAsset({
1919
__packager_asset: true,

packages/react-native/Libraries/Image/__tests__/resolveAssetSource-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @format
99
*/
1010

11-
import type {PackagerAsset} from '../../../../assets-registry/registry';
11+
import type {PackagerAsset} from '../../../src/private/assets/AssetRegistry';
1212
import type {ResolvedAssetSource} from '../AssetSourceResolver';
1313

1414
describe('resolveAssetSource', () => {
@@ -20,7 +20,8 @@ describe('resolveAssetSource', () => {
2020
beforeEach(() => {
2121
jest.resetModules();
2222

23-
AssetRegistry = require('@react-native/assets-registry/registry');
23+
AssetRegistry =
24+
require('../../../src/private/assets/AssetRegistry').AssetRegistry;
2425
resolveAssetSource = require('../resolveAssetSource').default;
2526
NativeSourceCode =
2627
require('../../NativeModules/specs/NativeSourceCode').default;

packages/react-native/Libraries/Image/resolveAssetSource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import type {ImageSource} from './ImageSource';
1616

1717
import SourceCode from '../NativeModules/specs/NativeSourceCode';
1818

19+
const {AssetRegistry} = require('../../src/private/assets/AssetRegistry');
1920
const AssetSourceResolver: AssetSourceResolverT =
2021
require('./AssetSourceResolver').default;
2122
const {pickScale} = require('./AssetUtils');
22-
const AssetRegistry = require('@react-native/assets-registry/registry');
2323

2424
type CustomSourceTransformer = (
2525
resolver: AssetSourceResolver,

packages/react-native/ReactNativeApi.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<42200de8ca10d30541e23b67547d9a13>>
7+
* @generated SignedSource<<51a4ff32766e21cd2638ff58450f31f9>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -151,6 +151,10 @@ declare const AnimatedScrollView_default: AnimatedComponentType<
151151
>
152152
declare const AppState: typeof AppState_default
153153
declare const AppState_default: AppStateImpl
154+
declare const AssetRegistry: {
155+
getAssetByID(assetId: number): PackagerAsset
156+
registerAsset(asset: PackagerAsset): number
157+
}
154158
declare const attachNativeEvent: typeof $$AnimatedImplementation.attachNativeEvent
155159
declare const BackHandler: typeof BackHandler_default
156160
declare const BackHandler_default: TBackHandler
@@ -1638,6 +1642,8 @@ declare interface ArrayLike_2<T> extends Iterable<T> {
16381642
[indexer: number]: T
16391643
readonly length: number
16401644
}
1645+
declare type AssetDestPathResolver = "android" | "generic"
1646+
declare type AssetRegistry = typeof AssetRegistry
16411647
declare type attachNativeEvent = typeof attachNativeEvent
16421648
declare function attachNativeEventImpl(
16431649
viewRef: any,
@@ -3479,6 +3485,17 @@ declare type OptionalVirtualizedSectionListProps<
34793485
declare type OrientationChangeEvent = {
34803486
readonly orientation: "landscape" | "portrait"
34813487
}
3488+
declare type PackagerAsset = {
3489+
readonly fileSystemLocation: string
3490+
readonly hash: string
3491+
readonly height: number | undefined
3492+
readonly httpServerLocation: string
3493+
readonly name: string
3494+
readonly resolver?: AssetDestPathResolver
3495+
readonly scales: Array<number>
3496+
readonly type: string
3497+
readonly width: number | undefined
3498+
}
34823499
declare type PanResponder = typeof PanResponder
34833500
declare type PanResponderCallbacks = {
34843501
readonly onMoveShouldSetPanResponder?: ActiveCallback
@@ -5927,6 +5944,8 @@ export {
59275944
AppStateEvent, // 80f034c3
59285945
AppStateStatus, // 447e5ef2
59295946
Appearance, // 83e9641a
5947+
AssetDestPathResolver, // 59047424
5948+
AssetRegistry, // 6070bb45
59305949
AutoCapitalize, // c0e857a0
59315950
BackHandler, // f139fc69
59325951
BackPressEventName, // 4620fb76
@@ -6060,6 +6079,7 @@ export {
60606079
NativeUIEvent, // 44ac26ac
60616080
Networking, // bbc5be42
60626081
OpaqueColorValue, // 25f3fa5b
6082+
PackagerAsset, // d1c88cf4
60636083
PanResponder, // f8f71cac
60646084
PanResponderCallbacks, // 6d63e7be
60656085
PanResponderGestureState, // 54baf558

0 commit comments

Comments
 (0)