universe-code 0.0.13 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/indexdb/db.js +4 -2
- package/dist/indexdb/index.js +2 -11
- package/dist/indexdb/store.js +22 -22
- package/package.json +1 -1
package/dist/indexdb/db.js
CHANGED
|
@@ -11,8 +11,9 @@ export class DBManager {
|
|
|
11
11
|
return new Promise((resolve, reject) => {
|
|
12
12
|
const request = indexedDB.open(this.dbName, this.version);
|
|
13
13
|
|
|
14
|
-
request.onupgradeneeded = (
|
|
15
|
-
const db =
|
|
14
|
+
request.onupgradeneeded = ({ target }) => {
|
|
15
|
+
const db = target.result;
|
|
16
|
+
|
|
16
17
|
stores.forEach(({ name, keyPath = "id", autoIncrement = false }) => {
|
|
17
18
|
if (!db.objectStoreNames.contains(name)) {
|
|
18
19
|
db.createObjectStore(name, { keyPath, autoIncrement });
|
|
@@ -24,6 +25,7 @@ export class DBManager {
|
|
|
24
25
|
this.db = request.result;
|
|
25
26
|
resolve(this.db);
|
|
26
27
|
};
|
|
28
|
+
|
|
27
29
|
request.onerror = () => reject(request.error);
|
|
28
30
|
});
|
|
29
31
|
}
|
package/dist/indexdb/index.js
CHANGED
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// Export everything as a unified object or individual pieces
|
|
5
|
-
export { DBManager, DB };
|
|
6
|
-
|
|
7
|
-
// Default export for easy usage
|
|
8
|
-
export default {
|
|
9
|
-
DBManager,
|
|
10
|
-
...DB
|
|
11
|
-
};
|
|
1
|
+
export { DBManager } from "./db.js";
|
|
2
|
+
export { Store } from "./store.js";
|
package/dist/indexdb/store.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
//
|
|
2
|
-
const perform = (db, storeName, mode, action) =>
|
|
3
|
-
|
|
1
|
+
// Single internal helper (never repeated)
|
|
2
|
+
const perform = (db, storeName, mode, action) =>
|
|
3
|
+
new Promise((resolve, reject) => {
|
|
4
4
|
const tx = db.transaction(storeName, mode);
|
|
5
5
|
const store = tx.objectStore(storeName);
|
|
6
6
|
const request = action(store);
|
|
7
|
+
|
|
7
8
|
request.onsuccess = () => resolve(request.result);
|
|
8
9
|
request.onerror = () => reject(request.error);
|
|
9
10
|
});
|
|
10
|
-
};
|
|
11
11
|
|
|
12
12
|
export class Store {
|
|
13
13
|
constructor(db, storeName) {
|
|
@@ -15,48 +15,48 @@ export class Store {
|
|
|
15
15
|
this.storeName = storeName;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return await perform(this.db, this.storeName, "readonly", s => s.get(key));
|
|
18
|
+
get(key) {
|
|
19
|
+
return perform(this.db, this.storeName, "readonly", s => s.get(key));
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
return
|
|
22
|
+
put(data) {
|
|
23
|
+
return perform(this.db, this.storeName, "readwrite", s => s.put(data));
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
return
|
|
26
|
+
remove(key) {
|
|
27
|
+
return perform(this.db, this.storeName, "readwrite", s => s.delete(key));
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
return
|
|
30
|
+
clear() {
|
|
31
|
+
return perform(this.db, this.storeName, "readwrite", s => s.clear());
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
/**
|
|
36
|
-
* Smart Fetch with TTL
|
|
35
|
+
* Smart Fetch with TTL caching
|
|
37
36
|
*/
|
|
38
37
|
async fetch(key, ttl, apiCall) {
|
|
38
|
+
const now = Date.now();
|
|
39
|
+
|
|
39
40
|
try {
|
|
40
41
|
const cached = await this.get(key);
|
|
41
|
-
const now = Date.now();
|
|
42
42
|
|
|
43
|
-
if (cached?.timestamp &&
|
|
43
|
+
if (cached?.timestamp && now - cached.timestamp < ttl) {
|
|
44
44
|
return cached.data;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
const
|
|
47
|
+
const data = await apiCall();
|
|
48
48
|
|
|
49
49
|
await this.put({
|
|
50
50
|
id: key,
|
|
51
|
-
data
|
|
51
|
+
data,
|
|
52
52
|
timestamp: now
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
-
return
|
|
56
|
-
} catch (
|
|
57
|
-
console.warn("
|
|
55
|
+
return data;
|
|
56
|
+
} catch (err) {
|
|
57
|
+
console.warn("IndexedDB fallback used:", err);
|
|
58
58
|
const fallback = await this.get(key);
|
|
59
|
-
return fallback
|
|
59
|
+
return fallback?.data ?? null;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|