safeguard for missing ImageLoader native module

Differential Revision: D3604593

fbshipit-source-id: f7f77bfb01ca51e660b68945ff7271a492df1bcb
This commit is contained in:
Olivier Notteghem 2016-07-22 13:09:35 -07:00 committed by Facebook Github Bot 8
parent 55638f804d
commit 32a717892c
1 changed files with 12 additions and 1 deletions

View File

@ -23,6 +23,7 @@ var StyleSheet = require('StyleSheet');
var StyleSheetPropType = require('StyleSheetPropType');
var View = require('View');
var emptyFunction = require('emptyFunction');
var flattenStyle = require('flattenStyle');
var merge = require('merge');
var requireNativeComponent = require('requireNativeComponent');
@ -140,6 +141,12 @@ var Image = React.createClass({
success: (width: number, height: number) => void,
failure: (error: any) => void,
) {
if (!ImageLoader) {
failure ?
failure() :
console.warn('ImageLoader native module is missing, image size cannot be fetched.');
return;
}
return ImageLoader.getSize(url)
.then(function(sizes) {
success(sizes.width, sizes.height);
@ -154,6 +161,10 @@ var Image = React.createClass({
* cache
*/
prefetch(url: string, callback: ?Function) {
if (!ImageLoader) {
console.warn('ImageLoader native module is missing, image will not preload.');
return new Promise(emptyFunction);
}
const requestId = generateRequestId();
callback && callback(requestId);
return ImageLoader.prefetchImage(url, requestId);
@ -163,7 +174,7 @@ var Image = React.createClass({
* Abort prefetch request
*/
abortPrefetch(requestId: number) {
ImageLoader.abortRequest(requestId);
ImageLoader && ImageLoader.abortRequest(requestId);
},
},