1 | import { getParameterError, shouldBeObject, temporarilyNotSupport } from '../../utils/index.js';
|
2 | import { MethodHandler } from '../../utils/handler.js';
|
3 | export { getBackgroundFetchData, getBackgroundFetchToken, onBackgroundFetchData, setBackgroundFetchToken } from './background-fetch.js';
|
4 | export { createCacheManager } from './cache-manager.js';
|
5 |
|
6 | function getItem(key) {
|
7 | let item;
|
8 | try {
|
9 | item = JSON.parse(localStorage.getItem(key) || '');
|
10 | }
|
11 | catch (e) { }
|
12 |
|
13 | if (item && typeof item === 'object' && item.hasOwnProperty('data')) {
|
14 | return { result: true, data: item.data };
|
15 | }
|
16 | else {
|
17 | return { result: false };
|
18 | }
|
19 | }
|
20 |
|
21 | const setStorageSync = (key, data = '') => {
|
22 | if (typeof key !== 'string') {
|
23 | console.error(getParameterError({
|
24 | name: 'setStorage',
|
25 | correct: 'String',
|
26 | wrong: key
|
27 | }));
|
28 | return;
|
29 | }
|
30 | const type = typeof data;
|
31 | let obj = {};
|
32 | if (type === 'symbol') {
|
33 | obj = { data: '' };
|
34 | }
|
35 | else {
|
36 | obj = { data };
|
37 | }
|
38 | localStorage.setItem(key, JSON.stringify(obj));
|
39 | };
|
40 | const setStorage = (options) => {
|
41 |
|
42 | const isObject = shouldBeObject(options);
|
43 | if (!isObject.flag) {
|
44 | const res = { errMsg: `setStorage:fail ${isObject.msg}` };
|
45 | console.error(res.errMsg);
|
46 | return Promise.reject(res);
|
47 | }
|
48 | const { key, data, success, fail, complete } = options;
|
49 | const handle = new MethodHandler({ name: 'setStorage', success, fail, complete });
|
50 | if (typeof key !== 'string') {
|
51 | return handle.fail({
|
52 | errMsg: getParameterError({
|
53 | para: 'key',
|
54 | correct: 'String',
|
55 | wrong: key
|
56 | })
|
57 | });
|
58 | }
|
59 | setStorageSync(key, data);
|
60 | return handle.success();
|
61 | };
|
62 | const revokeBufferURL = temporarilyNotSupport('revokeBufferURL');
|
63 | const removeStorageSync = (key) => {
|
64 | if (typeof key !== 'string') {
|
65 | console.error(getParameterError({
|
66 | name: 'removeStorage',
|
67 | correct: 'String',
|
68 | wrong: key
|
69 | }));
|
70 | return;
|
71 | }
|
72 | localStorage.removeItem(key);
|
73 | };
|
74 | const removeStorage = (options) => {
|
75 |
|
76 | const isObject = shouldBeObject(options);
|
77 | if (!isObject.flag) {
|
78 | const res = { errMsg: `removeStorage:fail ${isObject.msg}` };
|
79 | console.error(res.errMsg);
|
80 | return Promise.reject(res);
|
81 | }
|
82 | const { key, success, fail, complete } = options;
|
83 | const handle = new MethodHandler({ name: 'removeStorage', success, fail, complete });
|
84 | if (typeof key !== 'string') {
|
85 | return handle.fail({
|
86 | errMsg: getParameterError({
|
87 | para: 'key',
|
88 | correct: 'String',
|
89 | wrong: key
|
90 | })
|
91 | });
|
92 | }
|
93 | removeStorageSync(key);
|
94 | return handle.success();
|
95 | };
|
96 | const getStorageSync = (key) => {
|
97 | if (typeof key !== 'string') {
|
98 | console.error(getParameterError({
|
99 | name: 'getStorageSync',
|
100 | correct: 'String',
|
101 | wrong: key
|
102 | }));
|
103 | return;
|
104 | }
|
105 | const res = getItem(key);
|
106 | if (res.result)
|
107 | return res.data;
|
108 | return '';
|
109 | };
|
110 | const getStorageInfoSync = () => {
|
111 | const res = {
|
112 | keys: Object.keys(localStorage),
|
113 | limitSize: NaN,
|
114 | currentSize: NaN
|
115 | };
|
116 | return res;
|
117 | };
|
118 | const getStorageInfo = ({ success, fail, complete } = {}) => {
|
119 | const handle = new MethodHandler({ name: 'getStorageInfo', success, fail, complete });
|
120 | return handle.success(getStorageInfoSync());
|
121 | };
|
122 | const getStorage = (options) => {
|
123 |
|
124 | const isObject = shouldBeObject(options);
|
125 | if (!isObject.flag) {
|
126 | const res = { errMsg: `getStorage:fail ${isObject.msg}` };
|
127 | console.error(res.errMsg);
|
128 | return Promise.reject(res);
|
129 | }
|
130 | const { key, success, fail, complete } = options;
|
131 | const handle = new MethodHandler({ name: 'getStorage', success, fail, complete });
|
132 | if (typeof key !== 'string') {
|
133 | return handle.fail({
|
134 | errMsg: getParameterError({
|
135 | para: 'key',
|
136 | correct: 'String',
|
137 | wrong: key
|
138 | })
|
139 | });
|
140 | }
|
141 | const { result, data } = getItem(key);
|
142 | if (result) {
|
143 | return handle.success({ data });
|
144 | }
|
145 | else {
|
146 | return handle.fail({
|
147 | errMsg: 'data not found'
|
148 | });
|
149 | }
|
150 | };
|
151 | const createBufferURL = temporarilyNotSupport('createBufferURL');
|
152 | const clearStorageSync = () => {
|
153 | localStorage.clear();
|
154 | };
|
155 | const clearStorage = ({ success, fail, complete } = {}) => {
|
156 | const handle = new MethodHandler({ name: 'clearStorage', success, fail, complete });
|
157 | clearStorageSync();
|
158 | return handle.success();
|
159 | };
|
160 | const batchSetStorageSync = temporarilyNotSupport('batchSetStorageSync');
|
161 | const batchSetStorage = temporarilyNotSupport('batchSetStorage');
|
162 | const batchGetStorageSync = temporarilyNotSupport('batchGetStorageSync');
|
163 | const batchGetStorage = temporarilyNotSupport('batchGetStorage');
|
164 |
|
165 | export { batchGetStorage, batchGetStorageSync, batchSetStorage, batchSetStorageSync, clearStorage, clearStorageSync, createBufferURL, getStorage, getStorageInfo, getStorageInfoSync, getStorageSync, removeStorage, removeStorageSync, revokeBufferURL, setStorage, setStorageSync };
|
166 |
|