loading.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { writable } from 'svelte/store';
  2. import { initSearch } from './search';
  3. import { initDeck } from './deck';
  4. import { setCardDb, setAltId } from '../data/cardDb';
  5. let isLoading = writable(true);
  6. async function fetchCardDb() {
  7. let localVer = localStorage.getItem('card_db_ver');
  8. try {
  9. // load card db
  10. let response = await fetch("https://raye.mistivia.com/card_db_parts/version");
  11. if (!response.ok) {
  12. throw new Error('Network response was not ok');
  13. }
  14. let data = await response.json();
  15. data = String(data)
  16. if (localVer === data && localStorage.getItem('card_db') !== null) {
  17. setCardDb(JSON.parse(localStorage.getItem('card_db')));
  18. } else {
  19. localVer = data;
  20. response = await fetch("https://raye.mistivia.com/card_db_parts/index.json");
  21. if (!response.ok) {
  22. throw new Error('Network response was not ok');
  23. }
  24. data = await response.json();
  25. let tasks = data.map((i)=>fetch('https://raye.mistivia.com/card_db_parts/' + i));
  26. let datas = await Promise.all(tasks);
  27. datas = await Promise.all(datas.map((x) => x.text()));
  28. data = JSON.parse(datas.join(''));
  29. setCardDb(data);
  30. localStorage.setItem('card_db_ver', localVer);
  31. localStorage.setItem('card_db', datas.join(''));
  32. }
  33. // load alt id
  34. response = await fetch("https://ygocdb.com/api/v0/idChangelog.jsonp");
  35. if (!response.ok) {
  36. throw new Error('Network response was not ok');
  37. }
  38. data = await response.json();
  39. setAltId(data);
  40. } catch (error) {
  41. console.log(error);
  42. alert("加载失败!请刷新重试");
  43. return;
  44. }
  45. isLoading.set(false);
  46. initDeck();
  47. initSearch();
  48. }
  49. fetchCardDb();
  50. export {
  51. isLoading,
  52. };