UNPKG

5.59 kBJavaScriptView Raw
1import { getParameterError, shouldBeObject, temporarilyNotSupport } from '../../utils/index.js';
2import { MethodHandler } from '../../utils/handler.js';
3export { getBackgroundFetchData, getBackgroundFetchToken, onBackgroundFetchData, setBackgroundFetchToken } from './background-fetch.js';
4export { createCacheManager } from './cache-manager.js';
5
6function getItem(key) {
7 let item;
8 try {
9 item = JSON.parse(localStorage.getItem(key) || '');
10 }
11 catch (e) { } // eslint-disable-line no-empty
12 // 只返回使用 Taro.setStorage API 存储的数据
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// 数据缓存
21const 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};
40const setStorage = (options) => {
41 // options must be an Object
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};
62const revokeBufferURL = /* @__PURE__ */ temporarilyNotSupport('revokeBufferURL');
63const 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};
74const removeStorage = (options) => {
75 // options must be an Object
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};
96const 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};
110const getStorageInfoSync = () => {
111 const res = {
112 keys: Object.keys(localStorage),
113 limitSize: NaN,
114 currentSize: NaN
115 };
116 return res;
117};
118const getStorageInfo = ({ success, fail, complete } = {}) => {
119 const handle = new MethodHandler({ name: 'getStorageInfo', success, fail, complete });
120 return handle.success(getStorageInfoSync());
121};
122const getStorage = (options) => {
123 // options must be an Object
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};
151const createBufferURL = /* @__PURE__ */ temporarilyNotSupport('createBufferURL');
152const clearStorageSync = () => {
153 localStorage.clear();
154};
155const clearStorage = ({ success, fail, complete } = {}) => {
156 const handle = new MethodHandler({ name: 'clearStorage', success, fail, complete });
157 clearStorageSync();
158 return handle.success();
159};
160const batchSetStorageSync = /* @__PURE__ */ temporarilyNotSupport('batchSetStorageSync');
161const batchSetStorage = /* @__PURE__ */ temporarilyNotSupport('batchSetStorage');
162const batchGetStorageSync = /* @__PURE__ */ temporarilyNotSupport('batchGetStorageSync');
163const batchGetStorage = /* @__PURE__ */ temporarilyNotSupport('batchGetStorage');
164
165export { batchGetStorage, batchGetStorageSync, batchSetStorage, batchSetStorageSync, clearStorage, clearStorageSync, createBufferURL, getStorage, getStorageInfo, getStorageInfoSync, getStorageSync, removeStorage, removeStorageSync, revokeBufferURL, setStorage, setStorageSync };
166//# sourceMappingURL=index.js.map