@esportsplus/web-storage 0.0.8 → 0.1.0
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 +22 -13
- package/build/index.js +9 -3
- package/build/local/index.d.ts +26 -15
- package/build/local/index.js +125 -78
- package/build/local/instance.d.ts +5 -0
- package/build/local/instance.js +20 -0
- package/build/local/object.d.ts +3 -0
- package/build/local/object.js +123 -0
- package/build/local/types.d.ts +7 -6
- package/build/local/types.js +10 -1
- package/package.json +8 -10
- package/src/local/index.ts +189 -135
- package/src/local/types.ts +6 -6
- package/tsconfig.json +6 -18
- package/webpack.typescript.config.js +24 -34
package/build/index.d.ts
CHANGED
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
import local from './local';
|
|
2
2
|
declare const _default: {
|
|
3
|
-
local: {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
local: (options?: LocalForageOptions) => {
|
|
4
|
+
instance: LocalForage;
|
|
5
|
+
iterate: <T, U>(iteratee: (value: T, key: string, iterationNumber: number) => U, callback?: ((err: any, result: U) => void) | undefined) => Promise<U>;
|
|
6
|
+
keys: (callback?: ((err: any, keys: string[]) => void) | undefined) => Promise<string[]>;
|
|
7
|
+
length: (callback?: ((err: any, numberOfKeys: number) => void) | undefined) => Promise<number>;
|
|
8
|
+
promises: (Promise<any> | (() => void))[];
|
|
9
|
+
assign(key: string, value: import("./local/types").Object): void;
|
|
10
|
+
clear(): Promise<void>;
|
|
11
|
+
delete(...keys: string[]): void;
|
|
12
|
+
entries(): Promise<import("./local/types").Object>;
|
|
13
|
+
filter(fn: Function): Promise<import("./local/types").Object>;
|
|
14
|
+
get(key: string, value?: any): Promise<any>;
|
|
15
|
+
has(...keys: string[]): Promise<boolean>;
|
|
16
|
+
only(...keys: string[]): Promise<import("./local/types").Object>;
|
|
17
|
+
pop(key: string): Promise<any>;
|
|
18
|
+
push(key: string, ...values: any[]): void;
|
|
19
|
+
replace(values: {
|
|
11
20
|
[key: string]: any;
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
}): void;
|
|
22
|
+
shift(key: string): Promise<any>;
|
|
23
|
+
set(key: string, value: any): void;
|
|
24
|
+
sync(): Promise<void>;
|
|
25
|
+
unshift(key: string, ...values: any[]): void;
|
|
17
26
|
};
|
|
18
27
|
};
|
|
19
28
|
export default _default;
|
package/build/index.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.local = void 0;
|
|
7
|
+
const local_1 = __importDefault(require("./local"));
|
|
8
|
+
exports.local = local_1.default;
|
|
9
|
+
exports.default = { local: local_1.default };
|
package/build/local/index.d.ts
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
|
-
import
|
|
2
|
-
declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { Object } from './types';
|
|
2
|
+
declare class Store {
|
|
3
|
+
instance: LocalForage;
|
|
4
|
+
iterate: LocalForage['iterate'];
|
|
5
|
+
keys: LocalForage['keys'];
|
|
6
|
+
length: LocalForage['length'];
|
|
7
|
+
promises: (Promise<any> | (() => void))[];
|
|
8
|
+
constructor(options?: LocalForageOptions);
|
|
9
|
+
assign(key: string, value: Object): void;
|
|
10
|
+
clear(): Promise<void>;
|
|
11
|
+
delete(...keys: string[]): void;
|
|
12
|
+
entries(): Promise<Object>;
|
|
13
|
+
filter(fn: Function): Promise<Object>;
|
|
14
|
+
get(key: string, value?: any): Promise<any>;
|
|
15
|
+
has(...keys: string[]): Promise<boolean>;
|
|
16
|
+
only(...keys: string[]): Promise<Object>;
|
|
17
|
+
pop(key: string): Promise<any>;
|
|
18
|
+
push(key: string, ...values: any[]): void;
|
|
19
|
+
replace(values: {
|
|
10
20
|
[key: string]: any;
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
21
|
+
}): void;
|
|
22
|
+
shift(key: string): Promise<any>;
|
|
23
|
+
set(key: string, value: any): void;
|
|
24
|
+
sync(): Promise<void>;
|
|
25
|
+
unshift(key: string, ...values: any[]): void;
|
|
26
|
+
}
|
|
27
|
+
declare const _default: (options?: LocalForageOptions) => Store;
|
|
17
28
|
export default _default;
|
package/build/local/index.js
CHANGED
|
@@ -1,91 +1,138 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function init(options = {}) {
|
|
5
|
-
localforage.config(Object.assign({ name: 'store' }, options, { driver }));
|
|
6
|
-
}
|
|
7
|
-
async function sync(key) {
|
|
8
|
-
let root = (key.split('.')[0] || '');
|
|
9
|
-
await localforage.setItem(root, dot.get(cache, root));
|
|
10
|
-
}
|
|
11
|
-
const clear = () => {
|
|
12
|
-
cache = {};
|
|
13
|
-
localforage.clear();
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
4
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const types_1 = require("./types");
|
|
7
|
+
const localforage_1 = __importDefault(require("localforage"));
|
|
8
|
+
class Store {
|
|
9
|
+
instance;
|
|
10
|
+
iterate;
|
|
11
|
+
keys;
|
|
12
|
+
length;
|
|
13
|
+
promises;
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
let driver, name = options.name || 'store';
|
|
16
|
+
switch ((options.driver || types_1.Driver.IndexedDB)) {
|
|
17
|
+
case types_1.Driver.LocalStorage:
|
|
18
|
+
driver = localforage_1.default.LOCALSTORAGE;
|
|
19
|
+
break;
|
|
20
|
+
default:
|
|
21
|
+
driver = localforage_1.default.INDEXEDDB;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
this.instance = localforage_1.default.createInstance(Object.assign(options, { driver, name }));
|
|
25
|
+
this.iterate = this.instance.iterate;
|
|
26
|
+
this.keys = this.instance.keys;
|
|
27
|
+
this.length = this.instance.length;
|
|
28
|
+
this.promises = [];
|
|
19
29
|
}
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
assign(key, value) {
|
|
31
|
+
this.promises.push(async () => {
|
|
32
|
+
let data = (await this.get(key)) || {};
|
|
33
|
+
await this.instance.setItem(key, Object.assign(data, value));
|
|
34
|
+
});
|
|
22
35
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return dot.get(cache, key);
|
|
36
|
+
clear() {
|
|
37
|
+
this.promises = [];
|
|
38
|
+
return this.instance.clear();
|
|
27
39
|
}
|
|
28
|
-
|
|
29
|
-
|
|
40
|
+
delete(...keys) {
|
|
41
|
+
this.promises.push(async () => {
|
|
42
|
+
for (let i = 0, n = keys.length; i < n; i++) {
|
|
43
|
+
await this.instance.removeItem(keys[i]);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
30
46
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
47
|
+
async entries() {
|
|
48
|
+
let values = {};
|
|
49
|
+
await this.instance.iterate((value, key) => {
|
|
50
|
+
values[key] = value;
|
|
51
|
+
});
|
|
52
|
+
return values;
|
|
35
53
|
}
|
|
36
|
-
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
54
|
+
async filter(fn) {
|
|
55
|
+
let values = {};
|
|
56
|
+
await this.instance.iterate((value, key) => {
|
|
57
|
+
let result = fn(value, key), stop = typeof result !== 'boolean';
|
|
58
|
+
if (stop || (!stop && result)) {
|
|
59
|
+
values[key] = value;
|
|
60
|
+
}
|
|
61
|
+
if (stop) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return values;
|
|
41
66
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
67
|
+
async get(key, value = null) {
|
|
68
|
+
let data = await this.instance.getItem(key);
|
|
69
|
+
if (data === null) {
|
|
70
|
+
if (typeof value === 'function') {
|
|
71
|
+
data = await value();
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
data = value;
|
|
75
|
+
}
|
|
76
|
+
if (data !== null) {
|
|
77
|
+
this.set(key, data);
|
|
78
|
+
}
|
|
47
79
|
}
|
|
48
|
-
if (
|
|
49
|
-
|
|
80
|
+
if (data === null) {
|
|
81
|
+
throw new Error(`'${key}' has not been set in Storage`);
|
|
50
82
|
}
|
|
83
|
+
return data;
|
|
51
84
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
85
|
+
async has(...keys) {
|
|
86
|
+
let haystack = await this.instance.keys();
|
|
87
|
+
for (let i = 0, n = keys.length; i < n; i++) {
|
|
88
|
+
if (haystack.includes(keys[i])) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
58
94
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
};
|
|
62
|
-
const push = async (key, value) => {
|
|
63
|
-
let values = await get(key, []);
|
|
64
|
-
if (!Array.isArray(values)) {
|
|
65
|
-
values = [values];
|
|
95
|
+
async only(...keys) {
|
|
96
|
+
return await this.filter((key) => keys.includes(key));
|
|
66
97
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
set(key, values[key]);
|
|
98
|
+
async pop(key) {
|
|
99
|
+
let value, values = (await this.get(key)) || [];
|
|
100
|
+
value = values.pop();
|
|
101
|
+
this.promises.push(this.instance.setItem(key, values));
|
|
102
|
+
return value;
|
|
73
103
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
104
|
+
push(key, ...values) {
|
|
105
|
+
this.promises.push(async () => {
|
|
106
|
+
let data = (await this.get(key)) || [];
|
|
107
|
+
data.push(...values);
|
|
108
|
+
await this.instance.setItem(key, data);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
replace(values) {
|
|
112
|
+
this.promises.push(async () => {
|
|
113
|
+
for (let key in values) {
|
|
114
|
+
await this.instance.setItem(key, values[key]);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async shift(key) {
|
|
119
|
+
let value, values = (await this.get(key)) || [];
|
|
120
|
+
value = values.shift();
|
|
121
|
+
this.promises.push(this.instance.setItem(key, values));
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
set(key, value) {
|
|
125
|
+
this.promises.push(this.instance.setItem(key, value));
|
|
126
|
+
}
|
|
127
|
+
async sync() {
|
|
128
|
+
await Promise.allSettled(this.promises.splice(0));
|
|
129
|
+
}
|
|
130
|
+
unshift(key, ...values) {
|
|
131
|
+
this.promises.push(async () => {
|
|
132
|
+
let data = (await this.get(key)) || [];
|
|
133
|
+
data.unshift(...values);
|
|
134
|
+
await this.instance.setItem(key, data);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.default = (options = {}) => new Store(options);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const types_1 = require("./types");
|
|
7
|
+
const localforage_1 = __importDefault(require("localforage"));
|
|
8
|
+
const create = (options = {}) => {
|
|
9
|
+
let driver, name = options.name || 'store';
|
|
10
|
+
switch ((options.driver || types_1.Driver.IndexedDB)) {
|
|
11
|
+
case types_1.Driver.LocalStorage:
|
|
12
|
+
driver = localforage_1.default.LOCALSTORAGE;
|
|
13
|
+
break;
|
|
14
|
+
default:
|
|
15
|
+
driver = localforage_1.default.INDEXEDDB;
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
return localforage_1.default.createInstance(Object.assign(options, { driver, name }));
|
|
19
|
+
};
|
|
20
|
+
exports.default = { create };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const instance_1 = __importDefault(require("./instance"));
|
|
7
|
+
function Store(options = {}) {
|
|
8
|
+
this.instance = instance_1.default.create(options);
|
|
9
|
+
this.iterate = this.instance.iterate;
|
|
10
|
+
this.keys = this.instance.keys;
|
|
11
|
+
this.length = this.instance.length;
|
|
12
|
+
this.promises = [];
|
|
13
|
+
}
|
|
14
|
+
Store.prototype = {
|
|
15
|
+
assign: function (key, value) {
|
|
16
|
+
this.promises.push(async () => {
|
|
17
|
+
let data = (await this.get(key)) || {};
|
|
18
|
+
await this.instance.setItem(key, Object.assign(data, value));
|
|
19
|
+
});
|
|
20
|
+
},
|
|
21
|
+
clear: function () {
|
|
22
|
+
this.promises = [];
|
|
23
|
+
return this.instance.clear();
|
|
24
|
+
},
|
|
25
|
+
delete: function (...keys) {
|
|
26
|
+
this.promises.push(async () => {
|
|
27
|
+
for (let i = 0, n = keys.length; i < n; i++) {
|
|
28
|
+
await this.instance.removeItem(keys[i]);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
entries: async function () {
|
|
33
|
+
let values = {};
|
|
34
|
+
await this.instance.iterate((value, key) => {
|
|
35
|
+
values[key] = value;
|
|
36
|
+
});
|
|
37
|
+
return values;
|
|
38
|
+
},
|
|
39
|
+
filter: async function (fn) {
|
|
40
|
+
let values = {};
|
|
41
|
+
await this.instance.iterate((value, key) => {
|
|
42
|
+
let result = fn(value, key), stop = typeof result !== 'boolean';
|
|
43
|
+
if (stop || (!stop && result)) {
|
|
44
|
+
values[key] = value;
|
|
45
|
+
}
|
|
46
|
+
if (stop) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return values;
|
|
51
|
+
},
|
|
52
|
+
get: async function (key, value = null) {
|
|
53
|
+
let data = await this.instance.getItem(key);
|
|
54
|
+
if (data === null) {
|
|
55
|
+
if (typeof value === 'function') {
|
|
56
|
+
data = await value();
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
data = value;
|
|
60
|
+
}
|
|
61
|
+
if (data !== null) {
|
|
62
|
+
this.set(key, data);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (data === null) {
|
|
66
|
+
throw new Error(`'${key}' has not been set in Storage`);
|
|
67
|
+
}
|
|
68
|
+
return data;
|
|
69
|
+
},
|
|
70
|
+
has: async function (...keys) {
|
|
71
|
+
let haystack = await this.instance.keys();
|
|
72
|
+
for (let i = 0, n = keys.length; i < n; i++) {
|
|
73
|
+
if (haystack.includes(keys[i])) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
},
|
|
80
|
+
only: async function (...keys) {
|
|
81
|
+
return await this.filter((key) => keys.includes(key));
|
|
82
|
+
},
|
|
83
|
+
pop: async function (key) {
|
|
84
|
+
let value, values = (await this.get(key)) || [];
|
|
85
|
+
value = values.pop();
|
|
86
|
+
this.promises.push(this.instance.setItem(key, values));
|
|
87
|
+
return value;
|
|
88
|
+
},
|
|
89
|
+
push: function (key, ...values) {
|
|
90
|
+
this.promises.push(async () => {
|
|
91
|
+
let data = (await this.get(key)) || [];
|
|
92
|
+
data.push(...values);
|
|
93
|
+
await this.instance.setItem(key, data);
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
replace: function (values) {
|
|
97
|
+
this.promises.push(async () => {
|
|
98
|
+
for (let key in values) {
|
|
99
|
+
await this.instance.setItem(key, values[key]);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
shift: async function (key) {
|
|
104
|
+
let value, values = (await this.get(key)) || [];
|
|
105
|
+
value = values.shift();
|
|
106
|
+
this.promises.push(this.instance.setItem(key, values));
|
|
107
|
+
return value;
|
|
108
|
+
},
|
|
109
|
+
set: function (key, value) {
|
|
110
|
+
this.promises.push(this.instance.setItem(key, value));
|
|
111
|
+
},
|
|
112
|
+
settle: async function () {
|
|
113
|
+
await Promise.allSettled(this.promises.splice(0));
|
|
114
|
+
},
|
|
115
|
+
unshift: function (key, ...values) {
|
|
116
|
+
this.promises.push(async () => {
|
|
117
|
+
let data = (await this.get(key)) || [];
|
|
118
|
+
data.unshift(...values);
|
|
119
|
+
await this.instance.setItem(key, data);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
exports.default = (options = {}) => new Store(options);
|
package/build/local/types.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
declare
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
declare enum Driver {
|
|
2
|
+
IndexedDB = 0,
|
|
3
|
+
LocalStorage = 1
|
|
4
|
+
}
|
|
5
|
+
declare type Object = {
|
|
6
|
+
[key: string]: any;
|
|
6
7
|
};
|
|
7
|
-
export
|
|
8
|
+
export { Driver, Object };
|
package/build/local/types.js
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Driver = void 0;
|
|
4
|
+
var Driver;
|
|
5
|
+
(function (Driver) {
|
|
6
|
+
Driver[Driver["IndexedDB"] = 0] = "IndexedDB";
|
|
7
|
+
Driver[Driver["LocalStorage"] = 1] = "LocalStorage";
|
|
8
|
+
})(Driver || (Driver = {}));
|
|
9
|
+
exports.Driver = Driver;
|
|
10
|
+
;
|
package/package.json
CHANGED
|
@@ -6,15 +6,13 @@
|
|
|
6
6
|
},
|
|
7
7
|
"description": "Web storage utility",
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"glob": "^
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"webpack": "^5.64.4",
|
|
17
|
-
"webpack-cli": "^4.9.1"
|
|
9
|
+
"glob": "^8.0.3",
|
|
10
|
+
"ts-loader": "^9.4.1",
|
|
11
|
+
"tsc-alias": "^1.7.0",
|
|
12
|
+
"tsconfig-paths-webpack-plugin": "^4.0.0",
|
|
13
|
+
"typescript": "^4.8.4",
|
|
14
|
+
"webpack": "^5.74.0",
|
|
15
|
+
"webpack-cli": "^4.10.0"
|
|
18
16
|
},
|
|
19
17
|
"main": "./build/index.js",
|
|
20
18
|
"name": "@esportsplus/web-storage",
|
|
@@ -26,5 +24,5 @@
|
|
|
26
24
|
"prepublishOnly": "npm run build"
|
|
27
25
|
},
|
|
28
26
|
"types": "./build/index.d.ts",
|
|
29
|
-
"version": "0.0
|
|
27
|
+
"version": "0.1.0"
|
|
30
28
|
}
|
package/src/local/index.ts
CHANGED
|
@@ -1,135 +1,189 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
1
|
+
import { Driver, Object } from './types';
|
|
2
|
+
import localforage from 'localforage';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Store {
|
|
6
|
+
instance: LocalForage;
|
|
7
|
+
iterate: LocalForage['iterate'];
|
|
8
|
+
keys: LocalForage['keys'];
|
|
9
|
+
length: LocalForage['length'];
|
|
10
|
+
promises: (Promise<any> | (() => void))[];
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
constructor(options: LocalForageOptions = {}) {
|
|
14
|
+
let driver,
|
|
15
|
+
name = options.name || 'store';
|
|
16
|
+
|
|
17
|
+
switch ((options.driver || Driver.IndexedDB) as Driver) {
|
|
18
|
+
case Driver.LocalStorage:
|
|
19
|
+
driver = localforage.LOCALSTORAGE;
|
|
20
|
+
break;
|
|
21
|
+
default:
|
|
22
|
+
driver = localforage.INDEXEDDB;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
this.instance = localforage.createInstance( Object.assign(options, { driver, name }) );
|
|
27
|
+
this.iterate = this.instance.iterate;
|
|
28
|
+
this.keys = this.instance.keys;
|
|
29
|
+
this.length = this.instance.length;
|
|
30
|
+
this.promises = [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
assign(key: string, value: Object): void {
|
|
35
|
+
this.promises.push(async () => {
|
|
36
|
+
let data = (await this.get(key)) || {};
|
|
37
|
+
|
|
38
|
+
await this.instance.setItem(
|
|
39
|
+
key,
|
|
40
|
+
Object.assign(data, value)
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
clear(): Promise<void> {
|
|
46
|
+
this.promises = [];
|
|
47
|
+
|
|
48
|
+
return this.instance.clear();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
delete(...keys: string[]): void {
|
|
52
|
+
this.promises.push(async () => {
|
|
53
|
+
for (let i = 0, n = keys.length; i < n; i++) {
|
|
54
|
+
await this.instance.removeItem(keys[i]);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async entries(): Promise<Object> {
|
|
60
|
+
let values: Object = {};
|
|
61
|
+
|
|
62
|
+
await this.instance.iterate((value: any, key: string) => {
|
|
63
|
+
values[key] = value;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return values;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async filter(fn: Function): Promise<Object> {
|
|
70
|
+
let values: Object = {};
|
|
71
|
+
|
|
72
|
+
await this.instance.iterate((value: any, key: string) => {
|
|
73
|
+
let result = fn(value, key),
|
|
74
|
+
stop = typeof result !== 'boolean';
|
|
75
|
+
|
|
76
|
+
if (stop || (!stop && result)) {
|
|
77
|
+
values[key] = value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (stop) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return values;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async get(key: string, value: any = null): Promise<any> {
|
|
89
|
+
let data: any = await this.instance.getItem(key);
|
|
90
|
+
|
|
91
|
+
if (data === null) {
|
|
92
|
+
if (typeof value === 'function') {
|
|
93
|
+
data = await value();
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
data = value;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (data !== null) {
|
|
100
|
+
this.set(key, data);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (data === null) {
|
|
105
|
+
throw new Error(`'${key}' has not been set in Storage`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async has(...keys: string[]): Promise<boolean> {
|
|
112
|
+
let haystack = await this.instance.keys();
|
|
113
|
+
|
|
114
|
+
for (let i = 0, n = keys.length; i < n; i++) {
|
|
115
|
+
if (haystack.includes(keys[i])) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async only(...keys: string[]): Promise<Object> {
|
|
126
|
+
return await this.filter((key: string) => keys.includes(key));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async pop(key: string): Promise<any> {
|
|
130
|
+
let value,
|
|
131
|
+
values = (await this.get(key)) || [];
|
|
132
|
+
|
|
133
|
+
value = values.pop();
|
|
134
|
+
|
|
135
|
+
this.promises.push( this.instance.setItem(key, values) );
|
|
136
|
+
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
push(key: string, ...values: any[]): void {
|
|
141
|
+
this.promises.push(async () => {
|
|
142
|
+
let data = (await this.get(key)) || [];
|
|
143
|
+
|
|
144
|
+
data.push(...values);
|
|
145
|
+
|
|
146
|
+
await this.instance.setItem(key, data);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
replace(values: { [key: string]: any }): void {
|
|
151
|
+
this.promises.push(async () => {
|
|
152
|
+
for (let key in values) {
|
|
153
|
+
await this.instance.setItem(key, values[key])
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async shift(key: string): Promise<any> {
|
|
159
|
+
let value,
|
|
160
|
+
values = (await this.get(key)) || [];
|
|
161
|
+
|
|
162
|
+
value = values.shift();
|
|
163
|
+
|
|
164
|
+
this.promises.push( this.instance.setItem(key, values) );
|
|
165
|
+
|
|
166
|
+
return value;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
set(key: string, value: any): void {
|
|
170
|
+
this.promises.push( this.instance.setItem(key, value) );
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async sync(): Promise<void> {
|
|
174
|
+
await Promise.allSettled(this.promises.splice(0));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
unshift(key: string, ...values: any[]): void {
|
|
178
|
+
this.promises.push(async () => {
|
|
179
|
+
let data = (await this.get(key)) || [];
|
|
180
|
+
|
|
181
|
+
data.unshift(...values);
|
|
182
|
+
|
|
183
|
+
await this.instance.setItem(key, data);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
export default (options: LocalForageOptions = {}): Store => new Store(options);
|
package/src/local/types.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -1,35 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"allowJs": true,
|
|
4
|
-
"allowUmdGlobalAccess": true,
|
|
5
4
|
"alwaysStrict": true,
|
|
6
5
|
"baseUrl": "src",
|
|
7
6
|
"declaration": true,
|
|
8
7
|
"declarationDir": "./build",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"lib": ["dom", "es7"],
|
|
12
|
-
"module": "esnext",
|
|
8
|
+
"lib": ["dom", "ESNext"],
|
|
9
|
+
"module": "NodeNext",
|
|
13
10
|
"moduleResolution": "node",
|
|
14
|
-
"noFallthroughCasesInSwitch": true,
|
|
15
|
-
"noImplicitAny": true,
|
|
16
|
-
"noImplicitReturns": true,
|
|
17
|
-
"noImplicitThis": true,
|
|
18
|
-
"noUncheckedIndexedAccess": true,
|
|
19
11
|
"noUnusedLocals": true,
|
|
20
12
|
"noUnusedParameters": true,
|
|
21
13
|
"outDir": "./build",
|
|
22
|
-
"paths": {
|
|
14
|
+
"paths": {
|
|
15
|
+
"~/*": ["*"]
|
|
16
|
+
},
|
|
23
17
|
"removeComments": true,
|
|
24
18
|
"resolveJsonModule": true,
|
|
25
|
-
"skipLibCheck": true,
|
|
26
|
-
"sourceMap": false,
|
|
27
19
|
"strict": true,
|
|
28
|
-
"
|
|
29
|
-
"strictFunctionTypes": true,
|
|
30
|
-
"strictNullChecks": true,
|
|
31
|
-
"strictPropertyInitialization": true,
|
|
32
|
-
"target": "esnext"
|
|
20
|
+
"target": "ESNext"
|
|
33
21
|
},
|
|
34
22
|
"exclude": ["node_modules"],
|
|
35
23
|
"include": ["src"],
|
|
@@ -1,64 +1,54 @@
|
|
|
1
|
-
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
|
|
2
1
|
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
|
|
3
2
|
const glob = require('glob');
|
|
4
|
-
const webpack = require('webpack');
|
|
5
3
|
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
let optimization = {
|
|
9
|
-
usedExports: false
|
|
10
|
-
};
|
|
11
|
-
|
|
5
|
+
module.exports = ({ directory, entry, filename, library, output, production }) => {
|
|
12
6
|
filename = filename || 'app';
|
|
13
|
-
production = production ?
|
|
7
|
+
production = production == 'true' ? true : false;
|
|
8
|
+
|
|
9
|
+
if (directory) {
|
|
10
|
+
entry = glob.sync(`${directory}/{,!(node_modules)/**/}!(webpack)*!(.d).{ts,js}`);
|
|
11
|
+
}
|
|
14
12
|
|
|
15
13
|
if (production) {
|
|
16
|
-
|
|
17
|
-
mangleWasmImports: false,
|
|
18
|
-
minimize: false,
|
|
19
|
-
usedExports: false
|
|
20
|
-
};
|
|
14
|
+
filename += '.min';
|
|
21
15
|
}
|
|
22
16
|
|
|
23
17
|
return {
|
|
24
18
|
entry: {
|
|
25
|
-
[
|
|
19
|
+
[filename]: entry
|
|
26
20
|
},
|
|
27
|
-
mode: (production ? '
|
|
21
|
+
mode: (production ? 'production' : 'development'),
|
|
28
22
|
module: {
|
|
29
23
|
rules: [
|
|
30
24
|
{
|
|
31
25
|
test: /\.tsx?$/,
|
|
32
26
|
use: 'ts-loader',
|
|
33
|
-
exclude: /node_modules
|
|
27
|
+
exclude: /node_modules/,
|
|
28
|
+
resolve: {
|
|
29
|
+
fullySpecified: false,
|
|
30
|
+
}
|
|
34
31
|
}
|
|
35
32
|
]
|
|
36
33
|
},
|
|
37
|
-
optimization
|
|
34
|
+
optimization: {
|
|
35
|
+
mangleWasmImports: production,
|
|
36
|
+
minimize: production,
|
|
37
|
+
usedExports: production
|
|
38
|
+
},
|
|
38
39
|
output: {
|
|
39
|
-
// fixes ReferenceError: window is not defined
|
|
40
|
-
globalObject: "(typeof self !== 'undefined' ? self : this)",
|
|
41
40
|
library: library || filename,
|
|
42
41
|
path: output,
|
|
43
42
|
},
|
|
44
|
-
plugins: [
|
|
45
|
-
new NodePolyfillPlugin(),
|
|
46
|
-
new webpack.ProvidePlugin({
|
|
47
|
-
Buffer: ['buffer', 'Buffer'],
|
|
48
|
-
process: 'process/browser'
|
|
49
|
-
})
|
|
50
|
-
],
|
|
51
43
|
resolve: {
|
|
52
44
|
extensions: ['.js', '.ts', '.tsx'],
|
|
53
|
-
|
|
54
|
-
fs: false
|
|
55
|
-
},
|
|
45
|
+
fullySpecified: false,
|
|
56
46
|
plugins: [
|
|
57
|
-
new TsconfigPathsPlugin(
|
|
47
|
+
new TsconfigPathsPlugin({
|
|
48
|
+
extensions: ['.js', '.ts', '.tsx']
|
|
49
|
+
})
|
|
58
50
|
]
|
|
59
|
-
}
|
|
51
|
+
},
|
|
52
|
+
watch: true
|
|
60
53
|
};
|
|
61
54
|
};
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
module.exports = config;
|