Skip to content

Commit a0165d1

Browse files
committed
v1.0.4 Storage
1 parent 222ec39 commit a0165d1

File tree

8 files changed

+201
-104
lines changed

8 files changed

+201
-104
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# 变更日志
22

3+
## 0.1.4 / 2021-04-13
4+
5+
- Storage 接口声明更新,新增过期时间参数
6+
- Storage 模块化
7+
- Storage 模块重构
8+
- 自动化测试覆盖率 94.29%
9+
310
## 0.1.2 / 2021-04-02
411

512
- Storage 接口声明更新,新增过期时间

dist/index.aio.js

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168

169169
/**
170170
* @author jdeseva
171-
* @date 2021.04.02
171+
* @date 2021.04.13
172172
* @description 本地存储类 v2
173173
* @homePage https://github.com/jsmini/util-tools
174174
*/
@@ -181,11 +181,7 @@
181181
function Storage(isSingleInstance) {
182182
if (isSingleInstance === void 0) { isSingleInstance = true; }
183183
this.isSingleInstance = isSingleInstance;
184-
this.store = {
185-
L: JSON.parse(localStorage.getItem('store') || '{}'),
186-
S: JSON.parse(sessionStorage.getItem('store') || '{}'),
187-
};
188-
// this.addListenerToState()
184+
this.store = this.addListenerToState(JSON.parse(sessionStorage.getItem('store') || '{}'), JSON.parse(localStorage.getItem('store') || '{}'));
189185
return this.singleInstance();
190186
}
191187
/**
@@ -204,25 +200,45 @@
204200
/**
205201
* 添加状态监听器
206202
*/
207-
Storage.prototype.addListenerToState = function () {
208-
Object.defineProperty(this.store, 'S', {
209-
enumerable: true,
210-
configurable: false,
211-
writable: true,
212-
set: function (value) {
213-
console.log(111, value, this.store);
214-
this.store = value;
203+
Storage.prototype.addListenerToState = function (session, local) {
204+
var _this = this;
205+
var L = new Proxy(local, {
206+
get: function (target, key) {
207+
_this.checkTimeout(local);
208+
return target[key];
209+
},
210+
set: function (target, key, value) {
211+
target[key] = value;
212+
localStorage.setItem('store', JSON.stringify(target));
213+
return true;
214+
}
215+
});
216+
var S = new Proxy(session, {
217+
get: function (target, key) {
218+
_this.checkTimeout(session);
219+
return target[key];
215220
},
216-
get: function () {
217-
console.log(222, this.store);
218-
return this.store;
221+
set: function (target, key, value) {
222+
target[key] = value;
223+
sessionStorage.setItem('store', JSON.stringify(target));
224+
return true;
219225
}
220226
});
227+
return { L: L, S: S };
221228
};
222229
/**
223230
* 检查储存的数据是否超时
224231
*/
225-
Storage.prototype.checkTimeout = function () { };
232+
Storage.prototype.checkTimeout = function (ref) {
233+
Object.keys(ref).map(function (p) {
234+
if (ref[p].hasOwnProperty('delay')) {
235+
if (Date.now() >= ref[p].overTime) {
236+
Reflect.deleteProperty(ref, p);
237+
}
238+
}
239+
});
240+
return ref;
241+
};
226242
/**
227243
* 检测数据类型
228244
* @param data 数据类型
@@ -273,7 +289,7 @@
273289
* @param target 储存方法 默认 `sessionStorage` 当该值为 `true` 代表 `localStorage`(类型转换后为`true`也算)
274290
*/
275291
Storage.prototype.set = function (ref, value, delay, target) {
276-
var _this = this;
292+
var _this_1 = this;
277293
var refType = this.checkType(ref);
278294
var Target = target ? this.store.L : this.store.S;
279295
if (refType === 'String') {
@@ -284,7 +300,9 @@
284300
Target[ref] = result;
285301
}
286302
else {
287-
Object.keys(ref).forEach(function (p) { return _this.set(p, ref[p]); });
303+
Object.keys(ref).forEach(function (p) {
304+
return _this_1.set(p, ref[p]);
305+
});
288306
}
289307
sessionStorage.setItem('store', JSON.stringify(this.store.S));
290308
localStorage.setItem('store', JSON.stringify(this.store.L));
@@ -295,23 +313,25 @@
295313
* @param target 目标,默认 `undefined`,代表 `sessionStorage`, 为 `true` 时代表 `localStorage`(类型转换后为`true`也算)
296314
*/
297315
Storage.prototype.remove = function (ref, target) {
298-
var _this = this;
316+
var _this_1 = this;
299317
var refType = this.checkType(ref);
300318
var Target = target ? this.store.L : this.store.S;
301319
if (refType === 'String') {
302320
Reflect.deleteProperty(Target, ref);
303321
}
304322
else {
305-
ref.forEach(function (p) { return _this.remove(p); });
323+
ref.forEach(function (p) { return _this_1.remove(p); });
306324
}
307325
};
308326
/**
309327
* 删除 `Storage` 中所有的数据
310328
*/
311329
Storage.prototype.removeAll = function () {
330+
sessionStorage.removeItem('store');
331+
localStorage.removeItem('store');
312332
this.store = {
313333
L: {},
314-
S: {}
334+
S: {},
315335
};
316336
};
317337
/**

dist/index.aio.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.esm.js

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ var __assign = function() {
162162

163163
/**
164164
* @author jdeseva
165-
* @date 2021.04.02
165+
* @date 2021.04.13
166166
* @description 本地存储类 v2
167167
* @homePage https://github.com/jsmini/util-tools
168168
*/
@@ -175,11 +175,7 @@ var Storage = /** @class */ (function () {
175175
function Storage(isSingleInstance) {
176176
if (isSingleInstance === void 0) { isSingleInstance = true; }
177177
this.isSingleInstance = isSingleInstance;
178-
this.store = {
179-
L: JSON.parse(localStorage.getItem('store') || '{}'),
180-
S: JSON.parse(sessionStorage.getItem('store') || '{}'),
181-
};
182-
// this.addListenerToState()
178+
this.store = this.addListenerToState(JSON.parse(sessionStorage.getItem('store') || '{}'), JSON.parse(localStorage.getItem('store') || '{}'));
183179
return this.singleInstance();
184180
}
185181
/**
@@ -198,25 +194,45 @@ var Storage = /** @class */ (function () {
198194
/**
199195
* 添加状态监听器
200196
*/
201-
Storage.prototype.addListenerToState = function () {
202-
Object.defineProperty(this.store, 'S', {
203-
enumerable: true,
204-
configurable: false,
205-
writable: true,
206-
set: function (value) {
207-
console.log(111, value, this.store);
208-
this.store = value;
197+
Storage.prototype.addListenerToState = function (session, local) {
198+
var _this = this;
199+
var L = new Proxy(local, {
200+
get: function (target, key) {
201+
_this.checkTimeout(local);
202+
return target[key];
203+
},
204+
set: function (target, key, value) {
205+
target[key] = value;
206+
localStorage.setItem('store', JSON.stringify(target));
207+
return true;
208+
}
209+
});
210+
var S = new Proxy(session, {
211+
get: function (target, key) {
212+
_this.checkTimeout(session);
213+
return target[key];
209214
},
210-
get: function () {
211-
console.log(222, this.store);
212-
return this.store;
215+
set: function (target, key, value) {
216+
target[key] = value;
217+
sessionStorage.setItem('store', JSON.stringify(target));
218+
return true;
213219
}
214220
});
221+
return { L: L, S: S };
215222
};
216223
/**
217224
* 检查储存的数据是否超时
218225
*/
219-
Storage.prototype.checkTimeout = function () { };
226+
Storage.prototype.checkTimeout = function (ref) {
227+
Object.keys(ref).map(function (p) {
228+
if (ref[p].hasOwnProperty('delay')) {
229+
if (Date.now() >= ref[p].overTime) {
230+
Reflect.deleteProperty(ref, p);
231+
}
232+
}
233+
});
234+
return ref;
235+
};
220236
/**
221237
* 检测数据类型
222238
* @param data 数据类型
@@ -267,7 +283,7 @@ var Storage = /** @class */ (function () {
267283
* @param target 储存方法 默认 `sessionStorage` 当该值为 `true` 代表 `localStorage`(类型转换后为`true`也算)
268284
*/
269285
Storage.prototype.set = function (ref, value, delay, target) {
270-
var _this = this;
286+
var _this_1 = this;
271287
var refType = this.checkType(ref);
272288
var Target = target ? this.store.L : this.store.S;
273289
if (refType === 'String') {
@@ -278,7 +294,9 @@ var Storage = /** @class */ (function () {
278294
Target[ref] = result;
279295
}
280296
else {
281-
Object.keys(ref).forEach(function (p) { return _this.set(p, ref[p]); });
297+
Object.keys(ref).forEach(function (p) {
298+
return _this_1.set(p, ref[p]);
299+
});
282300
}
283301
sessionStorage.setItem('store', JSON.stringify(this.store.S));
284302
localStorage.setItem('store', JSON.stringify(this.store.L));
@@ -289,23 +307,25 @@ var Storage = /** @class */ (function () {
289307
* @param target 目标,默认 `undefined`,代表 `sessionStorage`, 为 `true` 时代表 `localStorage`(类型转换后为`true`也算)
290308
*/
291309
Storage.prototype.remove = function (ref, target) {
292-
var _this = this;
310+
var _this_1 = this;
293311
var refType = this.checkType(ref);
294312
var Target = target ? this.store.L : this.store.S;
295313
if (refType === 'String') {
296314
Reflect.deleteProperty(Target, ref);
297315
}
298316
else {
299-
ref.forEach(function (p) { return _this.remove(p); });
317+
ref.forEach(function (p) { return _this_1.remove(p); });
300318
}
301319
};
302320
/**
303321
* 删除 `Storage` 中所有的数据
304322
*/
305323
Storage.prototype.removeAll = function () {
324+
sessionStorage.removeItem('store');
325+
localStorage.removeItem('store');
306326
this.store = {
307327
L: {},
308-
S: {}
328+
S: {},
309329
};
310330
};
311331
/**

0 commit comments

Comments
 (0)