@esportsplus/web-storage 0.0.3 → 0.0.4
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/build/local/index.js +22 -9
- package/package.json +1 -1
- package/src/local/index.ts +27 -12
package/build/local/index.js
CHANGED
|
@@ -4,35 +4,48 @@ let cache = {}, driver = localforage.LOCALSTORAGE;
|
|
|
4
4
|
function init(options = {}) {
|
|
5
5
|
localforage.config(Object.assign({ name: 'store' }, options, { driver }));
|
|
6
6
|
}
|
|
7
|
+
function sync(key) {
|
|
8
|
+
localforage.setItem((key.split('.')[0] || ''), dot.get(cache, key));
|
|
9
|
+
}
|
|
7
10
|
const clear = () => {
|
|
8
11
|
cache = {};
|
|
9
12
|
localforage.clear();
|
|
10
13
|
};
|
|
11
14
|
const del = (key) => {
|
|
12
15
|
dot.set(cache, key, undefined);
|
|
13
|
-
|
|
16
|
+
if (key.includes('.')) {
|
|
17
|
+
sync(key);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
localforage.removeItem(key);
|
|
21
|
+
}
|
|
14
22
|
};
|
|
15
23
|
const get = async (key, fallback = null) => {
|
|
16
|
-
if (
|
|
24
|
+
if (await has(key)) {
|
|
17
25
|
return dot.get(cache, key);
|
|
18
26
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
value = await fallback();
|
|
27
|
+
if (typeof fallback === 'function') {
|
|
28
|
+
set(key, await fallback());
|
|
22
29
|
}
|
|
23
|
-
|
|
24
|
-
value = dot.get(cache, key);
|
|
30
|
+
let value = dot.get(cache, key);
|
|
25
31
|
if (value === null) {
|
|
26
32
|
throw new Error(`'${key}' has not been set in storage`);
|
|
27
33
|
}
|
|
28
34
|
return value;
|
|
29
35
|
};
|
|
30
36
|
const has = async (key) => {
|
|
31
|
-
|
|
37
|
+
if (dot.has(cache, key)) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
let value = await localforage.getItem(key.split('.')[0] || '');
|
|
41
|
+
if (value !== null) {
|
|
42
|
+
set(key, value);
|
|
43
|
+
}
|
|
44
|
+
return value !== null;
|
|
32
45
|
};
|
|
33
46
|
const set = (key, value) => {
|
|
34
47
|
dot.set(cache, key, value);
|
|
35
|
-
|
|
48
|
+
sync(key);
|
|
36
49
|
};
|
|
37
50
|
const useIndexedDB = (options = {}) => {
|
|
38
51
|
driver = localforage.INDEXEDDB;
|
package/package.json
CHANGED
package/src/local/index.ts
CHANGED
|
@@ -11,6 +11,10 @@ function init(options: Options = {}): void {
|
|
|
11
11
|
localforage.config(Object.assign({ name: 'store' }, options, { driver }));
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
function sync(key: string) {
|
|
15
|
+
localforage.setItem((key.split('.')[0] || ''), dot.get(cache, key));
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
|
|
15
19
|
const clear = () => {
|
|
16
20
|
cache = {};
|
|
@@ -19,24 +23,25 @@ const clear = () => {
|
|
|
19
23
|
|
|
20
24
|
const del = (key: string): void => {
|
|
21
25
|
dot.set(cache, key, undefined);
|
|
22
|
-
|
|
26
|
+
|
|
27
|
+
if (key.includes('.')) {
|
|
28
|
+
sync(key);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
localforage.removeItem(key);
|
|
32
|
+
}
|
|
23
33
|
}
|
|
24
34
|
|
|
25
35
|
const get = async (key: string, fallback: any = null): Promise<any> => {
|
|
26
|
-
if (
|
|
36
|
+
if (await has(key)) {
|
|
27
37
|
return dot.get(cache, key);
|
|
28
38
|
}
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (value === null && typeof fallback === 'function' ) {
|
|
34
|
-
value = await fallback();
|
|
40
|
+
if (typeof fallback === 'function') {
|
|
41
|
+
set(key, await fallback());
|
|
35
42
|
}
|
|
36
43
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
value = dot.get(cache, key);
|
|
44
|
+
let value = dot.get(cache, key);
|
|
40
45
|
|
|
41
46
|
if (value === null) {
|
|
42
47
|
throw new Error(`'${key}' has not been set in storage`);
|
|
@@ -46,12 +51,22 @@ const get = async (key: string, fallback: any = null): Promise<any> => {
|
|
|
46
51
|
};
|
|
47
52
|
|
|
48
53
|
const has = async (key: string): Promise<boolean> => {
|
|
49
|
-
|
|
54
|
+
if (dot.has(cache, key)) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let value: any = await localforage.getItem(key.split('.')[0] || '');
|
|
59
|
+
|
|
60
|
+
if (value !== null) {
|
|
61
|
+
set(key, value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return value !== null;
|
|
50
65
|
};
|
|
51
66
|
|
|
52
67
|
const set = (key: string, value: any): void => {
|
|
53
68
|
dot.set(cache, key, value);
|
|
54
|
-
|
|
69
|
+
sync(key);
|
|
55
70
|
};
|
|
56
71
|
|
|
57
72
|
const useIndexedDB = (options: Options = {}): void => {
|