mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
[ReactNative] Register assets with AssetRegistry
This commit is contained in:
20
Libraries/Image/AssetRegistry.js
Normal file
20
Libraries/Image/AssetRegistry.js
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright 2004-present Facebook. All Rights Reserved.
|
||||
*
|
||||
* @providesModule AssetRegistry
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var assets = [];
|
||||
|
||||
function registerAsset(asset) {
|
||||
// `push` returns new array length, so the first asset will
|
||||
// get id 1 (not 0) to make the value truthy
|
||||
return assets.push(asset);
|
||||
}
|
||||
|
||||
function getAssetByID(assetId) {
|
||||
return assets[assetId - 1];
|
||||
}
|
||||
|
||||
module.exports = { registerAsset, getAssetByID };
|
||||
@@ -8,19 +8,24 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
jest.dontMock('../resolveAssetSource');
|
||||
jest
|
||||
.dontMock('AssetRegistry')
|
||||
.dontMock('../resolveAssetSource');
|
||||
|
||||
var resolveAssetSource;
|
||||
var SourceCode;
|
||||
var AssetRegistry;
|
||||
|
||||
function expectResolvesAsset(input, expectedSource) {
|
||||
expect(resolveAssetSource(input)).toEqual(expectedSource);
|
||||
var assetId = AssetRegistry.registerAsset(input);
|
||||
expect(resolveAssetSource(assetId)).toEqual(expectedSource);
|
||||
}
|
||||
|
||||
describe('resolveAssetSource', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModuleRegistry();
|
||||
SourceCode = require('NativeModules').SourceCode;
|
||||
AssetRegistry = require('AssetRegistry');
|
||||
resolveAssetSource = require('../resolveAssetSource');
|
||||
});
|
||||
|
||||
@@ -32,6 +37,22 @@ describe('resolveAssetSource', () => {
|
||||
expect(resolveAssetSource(source2)).toBe(source2);
|
||||
});
|
||||
|
||||
it('does not change deprecated assets', () => {
|
||||
expect(resolveAssetSource({
|
||||
isStatic: true,
|
||||
deprecated: true,
|
||||
width: 100,
|
||||
height: 200,
|
||||
uri: 'logo',
|
||||
})).toEqual({
|
||||
isStatic: true,
|
||||
deprecated: true,
|
||||
width: 100,
|
||||
height: 200,
|
||||
uri: 'logo',
|
||||
});
|
||||
});
|
||||
|
||||
it('ignores any weird data', () => {
|
||||
expect(resolveAssetSource(null)).toBe(null);
|
||||
expect(resolveAssetSource(42)).toBe(null);
|
||||
@@ -81,25 +102,6 @@ describe('resolveAssetSource', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('does not change deprecated assets', () => {
|
||||
expectResolvesAsset({
|
||||
__packager_asset: true,
|
||||
deprecated: true,
|
||||
fileSystemLocation: '/root/app/module/a',
|
||||
httpServerLocation: '/assets/module/a',
|
||||
width: 100,
|
||||
height: 200,
|
||||
scales: [1],
|
||||
hash: '5b6f00f',
|
||||
name: 'logo',
|
||||
type: 'png',
|
||||
}, {
|
||||
isStatic: true,
|
||||
width: 100,
|
||||
height: 200,
|
||||
uri: 'logo',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bundle was loaded from file', () => {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var AssetRegistry = require('AssetRegistry');
|
||||
var PixelRatio = require('PixelRatio');
|
||||
var SourceCode = require('NativeModules').SourceCode;
|
||||
|
||||
@@ -44,58 +45,47 @@ function pickScale(scales, deviceScale) {
|
||||
}
|
||||
|
||||
function resolveAssetSource(source) {
|
||||
if (!source || typeof source !== 'object') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!source.__packager_asset) {
|
||||
if (typeof source === 'object') {
|
||||
return source;
|
||||
}
|
||||
|
||||
// Deprecated assets are managed by Xcode for now,
|
||||
// just returning image name as `uri`
|
||||
// Examples:
|
||||
// require('image!deprecatd_logo_example')
|
||||
// require('./new-hotness-logo-example.png')
|
||||
if (source.deprecated) {
|
||||
return {
|
||||
width: source.width,
|
||||
height: source.height,
|
||||
isStatic: true,
|
||||
uri: source.name || source.uri, // TODO(frantic): remove uri
|
||||
};
|
||||
var asset = AssetRegistry.getAssetByID(source);
|
||||
if (asset) {
|
||||
return assetToImageSource(asset);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function assetToImageSource(asset) {
|
||||
// TODO(frantic): currently httpServerLocation is used both as
|
||||
// path in http URL and path within IPA. Should we have zipArchiveLocation?
|
||||
var path = source.httpServerLocation;
|
||||
var path = asset.httpServerLocation;
|
||||
if (path[0] === '/') {
|
||||
path = path.substr(1);
|
||||
}
|
||||
|
||||
var scale = pickScale(source.scales, PixelRatio.get());
|
||||
var scale = pickScale(asset.scales, PixelRatio.get());
|
||||
var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
|
||||
|
||||
var fileName = source.name + scaleSuffix + '.' + source.type;
|
||||
var fileName = asset.name + scaleSuffix + '.' + asset.type;
|
||||
var serverURL = getServerURL();
|
||||
if (serverURL) {
|
||||
return {
|
||||
width: source.width,
|
||||
height: source.height,
|
||||
width: asset.width,
|
||||
height: asset.height,
|
||||
uri: serverURL + path + '/' + fileName +
|
||||
'?hash=' + source.hash,
|
||||
'?hash=' + asset.hash,
|
||||
isStatic: false,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
width: source.width,
|
||||
height: source.height,
|
||||
width: asset.width,
|
||||
height: asset.height,
|
||||
uri: path + '/' + fileName,
|
||||
isStatic: true,
|
||||
};
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
module.exports = resolveAssetSource;
|
||||
|
||||
Reference in New Issue
Block a user