@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.
@@ -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
- localforage.removeItem(key);
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 (dot.has(cache, key)) {
24
+ if (await has(key)) {
17
25
  return dot.get(cache, key);
18
26
  }
19
- let root = `${key}`.split('.')[0], value = await localforage.getItem(root);
20
- if (value === null && typeof fallback === 'function') {
21
- value = await fallback();
27
+ if (typeof fallback === 'function') {
28
+ set(key, await fallback());
22
29
  }
23
- set(root, value);
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
- return dot.has(cache, key) || (await localforage.getItem(key)) === null;
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
- localforage.setItem(key, value);
48
+ sync(key);
36
49
  };
37
50
  const useIndexedDB = (options = {}) => {
38
51
  driver = localforage.INDEXEDDB;
package/package.json CHANGED
@@ -26,5 +26,5 @@
26
26
  "prepublishOnly": "npm run build"
27
27
  },
28
28
  "types": "./build/index.d.ts",
29
- "version": "0.0.3"
29
+ "version": "0.0.4"
30
30
  }
@@ -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
- localforage.removeItem(key);
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 (dot.has(cache, key)) {
36
+ if (await has(key)) {
27
37
  return dot.get(cache, key);
28
38
  }
29
39
 
30
- let root: any = `${key}`.split('.')[0],
31
- value: any = await localforage.getItem(root);
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
- set(root, value);
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
- return dot.has(cache, key) || (await localforage.getItem(key)) === null;
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
- localforage.setItem(key, value);
69
+ sync(key);
55
70
  };
56
71
 
57
72
  const useIndexedDB = (options: Options = {}): void => {