universe-code 0.0.11 → 0.0.13
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/core/index.js +1 -0
- package/dist/core/time.js +13 -0
- package/dist/indexdb/store.js +31 -24
- package/package.json +1 -1
package/dist/core/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './time.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts minutes to milliseconds
|
|
3
|
+
* @param {number} minutes
|
|
4
|
+
* @returns {number} milliseconds
|
|
5
|
+
*/
|
|
6
|
+
export const minutesToMs = (minutes) => minutes * 60 * 1000;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts hours to milliseconds
|
|
10
|
+
* @param {number} hours
|
|
11
|
+
* @returns {number} milliseconds
|
|
12
|
+
*/
|
|
13
|
+
export const hoursToMs = (hours) => hours * 60 * 60 * 1000;
|
package/dist/indexdb/store.js
CHANGED
|
@@ -1,45 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
* Generic Transaction Wrapper
|
|
3
|
-
*/
|
|
1
|
+
// Internal helper for transactions
|
|
4
2
|
const perform = (db, storeName, mode, action) => {
|
|
5
3
|
return new Promise((resolve, reject) => {
|
|
6
4
|
const tx = db.transaction(storeName, mode);
|
|
7
5
|
const store = tx.objectStore(storeName);
|
|
8
6
|
const request = action(store);
|
|
9
|
-
|
|
10
7
|
request.onsuccess = () => resolve(request.result);
|
|
11
8
|
request.onerror = () => reject(request.error);
|
|
12
9
|
});
|
|
13
10
|
};
|
|
14
11
|
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
export class Store {
|
|
13
|
+
constructor(db, storeName) {
|
|
14
|
+
this.db = db;
|
|
15
|
+
this.storeName = storeName;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Now these methods don't need db or storeName passed in!
|
|
19
|
+
async get(key) {
|
|
20
|
+
return await perform(this.db, this.storeName, "readonly", s => s.get(key));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async put(data) {
|
|
24
|
+
return await perform(this.db, this.storeName, "readwrite", s => s.put(data));
|
|
25
|
+
}
|
|
19
26
|
|
|
20
|
-
remove
|
|
27
|
+
async remove(key) {
|
|
28
|
+
return await perform(this.db, this.storeName, "readwrite", s => s.delete(key));
|
|
29
|
+
}
|
|
21
30
|
|
|
22
|
-
clear
|
|
31
|
+
async clear() {
|
|
32
|
+
return await perform(this.db, this.storeName, "readwrite", s => s.clear());
|
|
33
|
+
}
|
|
23
34
|
|
|
24
35
|
/**
|
|
25
|
-
*
|
|
26
|
-
* Automatically refreshes data from an API if the local version is too old.
|
|
36
|
+
* Smart Fetch with TTL
|
|
27
37
|
*/
|
|
28
|
-
async
|
|
38
|
+
async fetch(key, ttl, apiCall) {
|
|
29
39
|
try {
|
|
30
|
-
const cached = await this.get(
|
|
40
|
+
const cached = await this.get(key);
|
|
31
41
|
const now = Date.now();
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (cached && cached.timestamp && (now - cached.timestamp < ttl)) {
|
|
42
|
+
|
|
43
|
+
if (cached?.timestamp && (now - cached.timestamp < ttl)) {
|
|
35
44
|
return cached.data;
|
|
36
45
|
}
|
|
37
46
|
|
|
38
|
-
// Otherwise, call API
|
|
39
47
|
const freshData = await apiCall();
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
await this.put(db, storeName, {
|
|
48
|
+
|
|
49
|
+
await this.put({
|
|
43
50
|
id: key,
|
|
44
51
|
data: freshData,
|
|
45
52
|
timestamp: now
|
|
@@ -47,9 +54,9 @@ export const DB = {
|
|
|
47
54
|
|
|
48
55
|
return freshData;
|
|
49
56
|
} catch (error) {
|
|
50
|
-
console.warn("
|
|
51
|
-
const fallback = await this.get(
|
|
57
|
+
console.warn("Returning fallback data due to error:", error);
|
|
58
|
+
const fallback = await this.get(key);
|
|
52
59
|
return fallback ? fallback.data : null;
|
|
53
60
|
}
|
|
54
61
|
}
|
|
55
|
-
}
|
|
62
|
+
}
|