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
|
|
|
/**
|
|
|
|
* Tries to preload an image.
|
|
|
|
*
|
2019-08-30 16:39:06 +00:00
|
|
|
* @param {string | Object} src - Source of the avatar.
|
2019-06-26 14:08:23 +00:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2019-08-30 16:39:06 +00:00
|
|
|
export function preloadImage(src: string | Object): 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
|
|
|
return new Promise((resolve, reject) => {
|
2020-10-14 19:58:28 +00:00
|
|
|
fetch(src, { referrer: '' })
|
|
|
|
.then(response => {
|
|
|
|
if (response.ok) {
|
|
|
|
resolve(src);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
reject(e);
|
|
|
|
});
|
2019-06-26 14:08:23 +00:00
|
|
|
});
|
|
|
|
}
|