universe-code 0.0.14 → 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.
@@ -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 = (e) => {
15
- const db = e.target.result;
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
  }
@@ -1,4 +1,2 @@
1
- import { DBManager } from './db.js';
2
- import { Store } from './store.js';
3
-
4
- export { DBManager, Store };
1
+ export { DBManager } from "./db.js";
2
+ export { Store } from "./store.js";
@@ -1,13 +1,13 @@
1
- // Internal helper for transactions
2
- const perform = (db, storeName, mode, action) => {
3
- return new Promise((resolve, reject) => {
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
- // 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));
18
+ get(key) {
19
+ return perform(this.db, this.storeName, "readonly", s => s.get(key));
21
20
  }
22
21
 
23
- async put(data) {
24
- return await perform(this.db, this.storeName, "readwrite", s => s.put(data));
22
+ put(data) {
23
+ return perform(this.db, this.storeName, "readwrite", s => s.put(data));
25
24
  }
26
25
 
27
- async remove(key) {
28
- return await perform(this.db, this.storeName, "readwrite", s => s.delete(key));
26
+ remove(key) {
27
+ return perform(this.db, this.storeName, "readwrite", s => s.delete(key));
29
28
  }
30
29
 
31
- async clear() {
32
- return await perform(this.db, this.storeName, "readwrite", s => s.clear());
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 && (now - cached.timestamp < ttl)) {
43
+ if (cached?.timestamp && now - cached.timestamp < ttl) {
44
44
  return cached.data;
45
45
  }
46
46
 
47
- const freshData = await apiCall();
47
+ const data = await apiCall();
48
48
 
49
49
  await this.put({
50
50
  id: key,
51
- data: freshData,
51
+ data,
52
52
  timestamp: now
53
53
  });
54
54
 
55
- return freshData;
56
- } catch (error) {
57
- console.warn("Returning fallback data due to error:", error);
55
+ return data;
56
+ } catch (err) {
57
+ console.warn("IndexedDB fallback used:", err);
58
58
  const fallback = await this.get(key);
59
- return fallback ? fallback.data : null;
59
+ return fallback?.data ?? null;
60
60
  }
61
61
  }
62
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "universe-code",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "Universal utility functions for all JS frameworks",
5
5
  "license": "ISC",
6
6
  "type": "module",