Selaa lähdekoodia

cache card db in localStorage

Mistivia 1 kuukausi sitten
vanhempi
commit
a7488d695b
1 muutettua tiedostoa jossa 21 lisäystä ja 6 poistoa
  1. 21 6
      src/control/loading.js

+ 21 - 6
src/control/loading.js

@@ -6,18 +6,33 @@ import { setCardDb, setAltId } from '../data/cardDb';
 let isLoading = writable(true);
 
 async function fetchCardDb() {
+    let localVer = localStorage.getItem('card_db_ver');
     try {
         // load card db
-        let response = await fetch("https://raye.mistivia.com/card_db_parts/index.json");
+        let response = await fetch("https://raye.mistivia.com/card_db_parts/version");
         if (!response.ok) {
             throw new Error('Network response was not ok');
         }
         let data = await response.json();
-        let tasks = data.map((i)=>fetch('https://raye.mistivia.com/card_db_parts/' + i));
-        let datas = await Promise.all(tasks);
-        datas = await Promise.all(datas.map((x) => x.text()));
-        data = JSON.parse(datas.join(''));
-        setCardDb(data);
+        data = String(data)
+        if (localVer === data && localStorage.getItem('card_db') !== null) {
+            setCardDb(JSON.parse(localStorage.getItem('card_db')));
+        } else {
+            localVer = data;
+            response = await fetch("https://raye.mistivia.com/card_db_parts/index.json");
+            if (!response.ok) {
+                throw new Error('Network response was not ok');
+            }
+            data = await response.json();
+            let tasks = data.map((i)=>fetch('https://raye.mistivia.com/card_db_parts/' + i));
+            let datas = await Promise.all(tasks);
+            datas = await Promise.all(datas.map((x) => x.text()));
+            data = JSON.parse(datas.join(''));
+            setCardDb(data);
+            localStorage.setItem('card_db_ver', localVer);
+            localStorage.setItem('card_db', datas.join(''));
+        }
+
         // load alt id
         response = await fetch("https://ygocdb.com/api/v0/idChangelog.jsonp");
         if (!response.ok) {