2019-06-26 14:08:23 +00:00
|
|
|
|
|
|
|
// @flow
|
|
|
|
|
2019-07-16 10:23:01 +00:00
|
|
|
import { isIconUrl } from './functions';
|
|
|
|
|
2019-06-26 14:08:23 +00:00
|
|
|
declare var config: Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to preload an image.
|
|
|
|
*
|
|
|
|
* @param {string} src - Source of the avatar.
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
export function preloadImage(src: string): Promise<string> {
|
2019-07-16 10:23:01 +00:00
|
|
|
if (isIconUrl(src)) {
|
|
|
|
return Promise.resolve(src);
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:08:23 +00:00
|
|
|
if (typeof config === 'object' && config.disableThirdPartyRequests) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const image = document.createElement('img');
|
|
|
|
|
|
|
|
image.onload = () => resolve(src);
|
|
|
|
image.onerror = reject;
|
|
|
|
image.src = src;
|
|
|
|
});
|
|
|
|
}
|