universe-code 0.0.14 → 0.0.16
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/index.js +9 -2
- package/dist/indexdb/store.js +21 -28
- package/package.json +1 -1
package/dist/indexdb/index.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { DBManager } from './db.js';
|
|
2
|
-
import {
|
|
2
|
+
import { DB } from './store.js';
|
|
3
3
|
|
|
4
|
-
|
|
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
|
+
};
|
package/dist/indexdb/store.js
CHANGED
|
@@ -1,52 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generic Transaction Wrapper
|
|
3
|
+
*/
|
|
2
4
|
const perform = (db, storeName, mode, action) => {
|
|
3
5
|
return new Promise((resolve, reject) => {
|
|
4
6
|
const tx = db.transaction(storeName, mode);
|
|
5
7
|
const store = tx.objectStore(storeName);
|
|
6
8
|
const request = action(store);
|
|
9
|
+
|
|
7
10
|
request.onsuccess = () => resolve(request.result);
|
|
8
11
|
request.onerror = () => reject(request.error);
|
|
9
12
|
});
|
|
10
13
|
};
|
|
11
14
|
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
this.db = db;
|
|
15
|
-
this.storeName = storeName;
|
|
16
|
-
}
|
|
15
|
+
export const DB = {
|
|
16
|
+
get: (db, store, key) => perform(db, store, "readonly", s => s.get(key)),
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
async get(key) {
|
|
20
|
-
return await perform(this.db, this.storeName, "readonly", s => s.get(key));
|
|
21
|
-
}
|
|
18
|
+
put: (db, store, data) => perform(db, store, "readwrite", s => s.put(data)),
|
|
22
19
|
|
|
23
|
-
|
|
24
|
-
return await perform(this.db, this.storeName, "readwrite", s => s.put(data));
|
|
25
|
-
}
|
|
20
|
+
remove: (db, store, key) => perform(db, store, "readwrite", s => s.delete(key)),
|
|
26
21
|
|
|
27
|
-
|
|
28
|
-
return await perform(this.db, this.storeName, "readwrite", s => s.delete(key));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async clear() {
|
|
32
|
-
return await perform(this.db, this.storeName, "readwrite", s => s.clear());
|
|
33
|
-
}
|
|
22
|
+
clear: (db, store) => perform(db, store, "readwrite", s => s.clear()),
|
|
34
23
|
|
|
35
24
|
/**
|
|
36
|
-
*
|
|
25
|
+
* PRO FEATURE: getWithExpiry
|
|
26
|
+
* Automatically refreshes data from an API if the local version is too old.
|
|
37
27
|
*/
|
|
38
|
-
async
|
|
28
|
+
async getWithExpiry(db, storeName, key, ttl, apiCall) {
|
|
39
29
|
try {
|
|
40
|
-
const cached = await this.get(key);
|
|
30
|
+
const cached = await this.get(db, storeName, key);
|
|
41
31
|
const now = Date.now();
|
|
42
32
|
|
|
43
|
-
|
|
33
|
+
// If valid data exists and is NOT expired, return it
|
|
34
|
+
if (cached && cached.timestamp && (now - cached.timestamp < ttl)) {
|
|
44
35
|
return cached.data;
|
|
45
36
|
}
|
|
46
37
|
|
|
38
|
+
// Otherwise, call API
|
|
47
39
|
const freshData = await apiCall();
|
|
48
40
|
|
|
49
|
-
|
|
41
|
+
// Save to IndexedDB with new timestamp
|
|
42
|
+
await this.put(db, storeName, {
|
|
50
43
|
id: key,
|
|
51
44
|
data: freshData,
|
|
52
45
|
timestamp: now
|
|
@@ -54,9 +47,9 @@ export class Store {
|
|
|
54
47
|
|
|
55
48
|
return freshData;
|
|
56
49
|
} catch (error) {
|
|
57
|
-
console.warn("
|
|
58
|
-
const fallback = await this.get(key);
|
|
50
|
+
console.warn("API/DB Error, returning fallback if available:", error);
|
|
51
|
+
const fallback = await this.get(db, storeName, key);
|
|
59
52
|
return fallback ? fallback.data : null;
|
|
60
53
|
}
|
|
61
54
|
}
|
|
62
|
-
}
|
|
55
|
+
};
|