(src: string)
| 1 | export const cache: { [key: string]: Promise<HTMLImageElement> } = {}; |
| 2 | |
| 3 | export default async function preload(src: string): Promise<HTMLImageElement> { |
| 4 | if (cache.hasOwnProperty(src)) { |
| 5 | return cache[src]; |
| 6 | } |
| 7 | const promise = new Promise((resolve, reject) => { |
| 8 | const image = new Image(); |
| 9 | image.onload = () => resolve(image); |
| 10 | image.onerror = () => reject(new Error(`Could not load image at ${src}`)); |
| 11 | image.src = src; |
| 12 | }) as Promise<HTMLImageElement>; |
| 13 | cache[src] = promise; |
| 14 | return promise; |
| 15 | } |
no outgoing calls
no test coverage detected