universe-code 0.0.53 → 0.0.56
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/angular/index.js
CHANGED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DBManager, DB } from 'universe-code/indexdb';
|
|
2
|
+
|
|
3
|
+
export class idbService {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.manager = new DBManager('exchange', 1);
|
|
6
|
+
this.db = null;
|
|
7
|
+
this.storeName = 'exchangeStore';
|
|
8
|
+
this.store = null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async getStore() {
|
|
12
|
+
if (!this.store) {
|
|
13
|
+
if (!this.db) {
|
|
14
|
+
this.db = await this.manager.connect([{ name: this.storeName }]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.store = {
|
|
18
|
+
get: (key) => DB.get(this.db, this.storeName, key),
|
|
19
|
+
getWithExpiry: (key, ttl, api) =>
|
|
20
|
+
DB.getWithExpiry(this.db, this.storeName, key, ttl, api),
|
|
21
|
+
put: (data) => DB.put(this.db, this.storeName, data),
|
|
22
|
+
remove: (key) => DB.remove(this.db, this.storeName, key),
|
|
23
|
+
clear: () => DB.clear(this.db, this.storeName)
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return this.store;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async fetchWithCache(key, ttl, apiFn) {
|
|
30
|
+
const store = await this.getStore();
|
|
31
|
+
return store.getWithExpiry(key, ttl, apiFn);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async get(key) {
|
|
35
|
+
return (await this.getStore()).get(key);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async set(data) {
|
|
39
|
+
return (await this.getStore()).put(data);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async remove(key) {
|
|
43
|
+
return (await this.getStore()).remove(key);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async clear() {
|
|
47
|
+
return (await this.getStore()).clear();
|
|
48
|
+
}
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core';
|
|
2
|
-
import { DBManager, DB } from 'universe-code/indexdb';
|
|
3
|
-
|
|
4
|
-
@Injectable({
|
|
5
|
-
providedIn: 'root'
|
|
6
|
-
})
|
|
7
|
-
export class idbService {
|
|
8
|
-
private manager = new DBManager('exchange', 1);
|
|
9
|
-
private db!: IDBDatabase;
|
|
10
|
-
private store: {
|
|
11
|
-
get: (key: string) => Promise<any>;
|
|
12
|
-
getWithExpiry: (
|
|
13
|
-
key: string,
|
|
14
|
-
ttl: number,
|
|
15
|
-
api: () => Promise<any>
|
|
16
|
-
) => Promise<any>;
|
|
17
|
-
put: (data: any) => Promise<any>;
|
|
18
|
-
remove: (key: string) => Promise<any>;
|
|
19
|
-
clear: () => Promise<any>;
|
|
20
|
-
} | null = null;
|
|
21
|
-
|
|
22
|
-
private readonly storeName = 'exchangeStore';
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Initialize DB + Store only once (Singleton)
|
|
26
|
-
*/
|
|
27
|
-
private async getStore() {
|
|
28
|
-
if (!this.store) {
|
|
29
|
-
if (!this.db) {
|
|
30
|
-
this.db = await this.manager.connect([
|
|
31
|
-
{ name: this.storeName }
|
|
32
|
-
]);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
this.store = {
|
|
36
|
-
get: (key: string) =>
|
|
37
|
-
DB.get(this.db, this.storeName, key),
|
|
38
|
-
|
|
39
|
-
getWithExpiry: (
|
|
40
|
-
key: string,
|
|
41
|
-
ttl: number,
|
|
42
|
-
api: () => Promise<any>
|
|
43
|
-
) =>
|
|
44
|
-
DB.getWithExpiry(this.db, this.storeName, key, ttl, api),
|
|
45
|
-
|
|
46
|
-
put: (data: any) =>
|
|
47
|
-
DB.put(this.db, this.storeName, data),
|
|
48
|
-
|
|
49
|
-
remove: (key: string) =>
|
|
50
|
-
DB.remove(this.db, this.storeName, key),
|
|
51
|
-
|
|
52
|
-
clear: () =>
|
|
53
|
-
DB.clear(this.db, this.storeName)
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return this.store;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/* =======================
|
|
61
|
-
PUBLIC METHODS
|
|
62
|
-
======================= */
|
|
63
|
-
|
|
64
|
-
async fetchWithCache(
|
|
65
|
-
key: string,
|
|
66
|
-
ttl: number,
|
|
67
|
-
apiFn: () => Promise<any>
|
|
68
|
-
) {
|
|
69
|
-
const store = await this.getStore();
|
|
70
|
-
return store.getWithExpiry(key, ttl, apiFn);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async get(key: string) {
|
|
74
|
-
const store = await this.getStore();
|
|
75
|
-
return store.get(key);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async set(data: any) {
|
|
79
|
-
const store = await this.getStore();
|
|
80
|
-
return store.put(data);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async remove(key: string) {
|
|
84
|
-
const store = await this.getStore();
|
|
85
|
-
return store.remove(key);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async clear() {
|
|
89
|
-
const store = await this.getStore();
|
|
90
|
-
return store.clear();
|
|
91
|
-
}
|
|
92
|
-
}
|