Correctly apply image timeout for loading images

This commit is contained in:
Niklas von Hertzen 2017-08-18 21:53:42 +08:00
parent c093c95881
commit bd463d9343
2 changed files with 12 additions and 3 deletions

View File

@ -78,10 +78,12 @@ export default class ImageLoader<T> {
} }
} }
}; };
xhr.ontimeout = () => reject(`Timed out fetching ${src}`);
xhr.responseType = 'blob'; xhr.responseType = 'blob';
if (this.options.imageTimeout) { if (this.options.imageTimeout) {
xhr.timeout = this.options.imageTimeout; const timeout = this.options.imageTimeout;
xhr.timeout = timeout;
xhr.ontimeout = () =>
reject(__DEV__ ? `Timed out (${timeout}ms) fetching ${src}` : '');
} }
xhr.open('GET', src, true); xhr.open('GET', src, true);
xhr.send(); xhr.send();
@ -122,6 +124,13 @@ export default class ImageLoader<T> {
resolve(img); resolve(img);
}, 500); }, 500);
} }
if (this.options.imageTimeout) {
const timeout = this.options.imageTimeout;
setTimeout(
() => reject(__DEV__ ? `Timed out (${timeout}ms) fetching ${src}` : ''),
timeout
);
}
}); });
this.cache[key] = this.cache[key] =

View File

@ -19,7 +19,7 @@ export type Options = {
async: ?boolean, async: ?boolean,
allowTaint: ?boolean, allowTaint: ?boolean,
canvas: ?HTMLCanvasElement, canvas: ?HTMLCanvasElement,
imageTimeout: ?number, imageTimeout: number,
proxy: ?string, proxy: ?string,
removeContainer: ?boolean, removeContainer: ?boolean,
scale: number, scale: number,