import { resources } from "./resources";

export function preload(onProgress) {
  return new Promise((resolve, reject) => {
    const total = resources.length;
    let cursor = 0;

    const callback = () => {
      cursor += 1;
      onProgress(cursor / total);
      if (cursor >= total) {
        resolve(resources)
      }
    }

    for (let res of resources) {
      const img = document.createElement('img');
      img.onload = callback;
      img.onerror = callback;
      img.src = res.image;
      res.img = img;
    }
  })
}
