@esportsplus/web-storage 0.0.1 → 0.0.2

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/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import local from './local';
2
2
  declare const _default: {
3
3
  local: {
4
- clear: (callback?: ((err: any) => void) | undefined) => Promise<void>;
4
+ clear: () => void;
5
5
  delete: (key: string) => void;
6
- get: (key: string) => Promise<any>;
6
+ get: (key: string, fallback?: any) => Promise<any>;
7
+ has: (key: string) => Promise<boolean>;
7
8
  set: (key: string, value: any) => void;
8
9
  useIndexedDB: (options?: import("./local/types").Options) => void;
9
10
  useLocalStorage: (options?: import("./local/types").Options) => void;
@@ -1,8 +1,9 @@
1
1
  import type { Options } from './types';
2
2
  declare const _default: {
3
- clear: (callback?: ((err: any) => void) | undefined) => Promise<void>;
3
+ clear: () => void;
4
4
  delete: (key: string) => void;
5
- get: (key: string) => Promise<any>;
5
+ get: (key: string, fallback?: any) => Promise<any>;
6
+ has: (key: string) => Promise<boolean>;
6
7
  set: (key: string, value: any) => void;
7
8
  useIndexedDB: (options?: Options) => void;
8
9
  useLocalStorage: (options?: Options) => void;
@@ -1,16 +1,36 @@
1
+ import dot from '@esportsplus/dot';
1
2
  import localforage from 'localforage';
2
- let driver = localforage.LOCALSTORAGE;
3
+ let cache = {}, driver = localforage.LOCALSTORAGE;
3
4
  function init(options = {}) {
4
5
  localforage.config(Object.assign({ name: 'store' }, options, { driver }));
5
6
  }
6
- const clear = localforage.clear;
7
+ const clear = () => {
8
+ cache = {};
9
+ localforage.clear();
10
+ };
7
11
  const del = (key) => {
12
+ cache[key] = undefined;
8
13
  localforage.removeItem(key);
9
14
  };
10
- const get = (key) => {
11
- return localforage.getItem(key);
15
+ const get = async (key, fallback = null) => {
16
+ if (dot.has(cache, key)) {
17
+ return dot.get(cache, key);
18
+ }
19
+ let value = await localforage.getItem(key);
20
+ if (value === null && typeof fallback === 'function') {
21
+ set(key, await fallback());
22
+ }
23
+ value = dot.get(cache, key) || value;
24
+ if (value === null) {
25
+ throw new Error(`'${key}' has not been set in storage`);
26
+ }
27
+ return value;
28
+ };
29
+ const has = async (key) => {
30
+ return dot.has(cache, key) || (await localforage.getItem(key)) === null;
12
31
  };
13
32
  const set = (key, value) => {
33
+ cache[key] = value;
14
34
  localforage.setItem(key, value);
15
35
  };
16
36
  const useIndexedDB = (options = {}) => {
@@ -25,4 +45,4 @@ const useOptions = (options = {}) => {
25
45
  init(options);
26
46
  };
27
47
  init();
28
- export default { clear, delete: del, get, set, useIndexedDB, useLocalStorage, useOptions };
48
+ export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
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.1"
29
+ "version": "0.0.2"
30
30
  }
@@ -1,46 +1,73 @@
1
- import type { Options } from './types';
2
- import localforage from 'localforage';
3
-
4
-
5
- let driver: any = localforage.LOCALSTORAGE;
6
-
7
-
8
- function init(options: Options = {}): void {
9
- localforage.config(Object.assign({ name: 'store' }, options, { driver }));
10
- }
11
-
12
-
13
- const clear = localforage.clear;
14
-
15
- const del = (key: string): void => {
16
- localforage.removeItem(key);
17
- }
18
-
19
- const get = (key: string): Promise<any> => {
20
- return localforage.getItem(key);
21
- };
22
-
23
- const set = (key: string, value: any): void => {
24
- localforage.setItem(key, value);
25
- };
26
-
27
- const useIndexedDB = (options: Options = {}): void => {
28
- driver = localforage.INDEXEDDB;
29
- init(options);
30
- };
31
-
32
- const useLocalStorage = (options: Options = {}): void => {
33
- driver = localforage.LOCALSTORAGE;
34
- init(options);
35
- };
36
-
37
- const useOptions = (options: Options = {}): void => {
38
- init(options);
39
- };
40
-
41
-
42
- // Initialize using localstorage as default storage
43
- init();
44
-
45
-
46
- export default { clear, delete: del, get, set, useIndexedDB, useLocalStorage, useOptions };
1
+ import type { Options } from './types';
2
+ import dot from '@esportsplus/dot';
3
+ import localforage from 'localforage';
4
+
5
+
6
+ let cache: any = {},
7
+ driver: any = localforage.LOCALSTORAGE;
8
+
9
+
10
+ function init(options: Options = {}): void {
11
+ localforage.config(Object.assign({ name: 'store' }, options, { driver }));
12
+ }
13
+
14
+
15
+ const clear = () => {
16
+ cache = {};
17
+ localforage.clear();
18
+ };
19
+
20
+ const del = (key: string): void => {
21
+ cache[key] = undefined;
22
+ localforage.removeItem(key);
23
+ }
24
+
25
+ const get = async (key: string, fallback: any = null): Promise<any> => {
26
+ if (dot.has(cache, key)) {
27
+ return dot.get(cache, key);
28
+ }
29
+
30
+ let value: any = await localforage.getItem(key);
31
+
32
+ if (value === null && typeof fallback === 'function' ) {
33
+ set(key, await fallback());
34
+ }
35
+
36
+ value = dot.get(cache, key) || value;
37
+
38
+ if (value === null) {
39
+ throw new Error(`'${key}' has not been set in storage`);
40
+ }
41
+
42
+ return value;
43
+ };
44
+
45
+ const has = async (key: string): Promise<boolean> => {
46
+ return dot.has(cache, key) || (await localforage.getItem(key)) === null;
47
+ };
48
+
49
+ const set = (key: string, value: any): void => {
50
+ cache[key] = value;
51
+ localforage.setItem(key, value);
52
+ };
53
+
54
+ const useIndexedDB = (options: Options = {}): void => {
55
+ driver = localforage.INDEXEDDB;
56
+ init(options);
57
+ };
58
+
59
+ const useLocalStorage = (options: Options = {}): void => {
60
+ driver = localforage.LOCALSTORAGE;
61
+ init(options);
62
+ };
63
+
64
+ const useOptions = (options: Options = {}): void => {
65
+ init(options);
66
+ };
67
+
68
+
69
+ // Initialize using localstorage as default storage
70
+ init();
71
+
72
+
73
+ export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };