[fix] limit Image loader deferral to 1000ms

This patch introduces a limit on how long image loading is deferred, and
mitigates an issue with lengthy delays to 'requestIdleCallback' in the
Chrome browser.

Fix #759
This commit is contained in:
Nicolas Gallagher
2018-01-10 16:31:13 -08:00
parent 902ba22877
commit 2117e44e9d
2 changed files with 13 additions and 6 deletions

View File

@@ -87,6 +87,9 @@ type State = {
shouldDisplaySource: boolean
};
const getAssetTimeout = source =>
typeof source === 'object' && source.timeout ? source.timeout : 1000;
class Image extends Component<*, State> {
static displayName = 'Image';
@@ -246,12 +249,16 @@ class Image extends Component<*, State> {
}
_createImageLoader() {
const { source } = this.props;
this._destroyImageLoader();
this._loadRequest = requestIdleCallback(() => {
const uri = resolveAssetSource(this.props.source);
this._imageRequestId = ImageLoader.load(uri, this._onLoad, this._onError);
this._onLoadStart();
});
this._loadRequest = requestIdleCallback(
() => {
const uri = resolveAssetSource(source);
this._imageRequestId = ImageLoader.load(uri, this._onLoad, this._onError);
this._onLoadStart();
},
{ timeout: getAssetTimeout(source) }
);
}
_destroyImageLoader() {

View File

@@ -10,7 +10,7 @@
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
const _requestIdleCallback = function(cb: Function) {
const _requestIdleCallback = function(cb: Function, options: Object) {
return setTimeout(() => {
const start = Date.now();
cb({