Merge pull request 'Add Storage Cacher' (#11) from dev-mrkbear into master

Reviewed-on: http://git.mrkbear.com/MrKBear/mini-dlpu-v3/pulls/11
This commit is contained in:
MrKBear 2021-12-13 19:51:11 +08:00
commit 9b4cfe95a5
5 changed files with 237 additions and 7 deletions

View File

@ -1,13 +1,15 @@
import { Logger } from "./core/Logger";
import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel";
import { Storage } from "./core/Storage";
App<IAppOption>({
App({
/**
*
*
*/
globalData: {},
storageCache: new Set<string>(),
/**
*
@ -15,5 +17,14 @@ App<IAppOption>({
onLaunch() {
Logger.log("小程序启动...",
LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLaunchLabel);
}
let s = new Storage("test", {
a: new Date(),
be: 2
});
setTimeout(() => {
s.set("be", 12);
}, 1000)
}
})

View File

@ -310,4 +310,13 @@ class LifeCycleLogLabel {
);
}
export { StackInfo, StackLogLabel, LevelLogLabel, LifeCycleLogLabel, normalLifeStyleGen as colorRadio };
const NormalStyle = StackLogLabel.normalStyle;
export {
NormalStyle,
StackInfo,
StackLogLabel,
LevelLogLabel,
LifeCycleLogLabel,
normalLifeStyleGen as colorRadio,
};

210
miniprogram/core/Storage.ts Normal file
View File

@ -0,0 +1,210 @@
import { LogLabel } from "./LogLabel";
import { Logger } from "./Logger";
import { LevelLogLabel, colorRadio } from "./PresetLogLabel";
/**
*
*/
enum StorageState {
/**
* waiter
*/
WORK = 1,
/**
* waiter
*/
DONE = 2
}
/**
*
*/
class Waiter {
/**
*
*/
public get state():StorageState {
if(this.waiterList.length <= 0) {
return StorageState.DONE;
} else {
return StorageState.WORK;
}
}
/**
*
*/
private waiterList:Function[] = [];
/**
*
* @param fn
*/
public addWaiter(fn:Function) {
this.waiterList.push(fn);
}
/**
*
* waiterList
* waiterList
*/
public done():Function {
// 复制 waiterList 生成闭包
let fnList = this.waiterList.concat([]);
let runAllWaiter = () => {
for(let i = 0; i < fnList.length; i++) {
fnList[i]();
}
}
this.waiterList = [];
return runAllWaiter;
}
}
/**
*
*/
type IStorageData = {
[x:string]:any
}
/**
*
* @template T
*
*
* 1. wxStorage
* 2.
* 3. 使
* 4.
*/
class Storage<T extends IStorageData> {
/**
* Logger 使
*/
public StorageLogLabel:LogLabel;
/**
*
*/
public key:string;
/**
*
*/
private readonly defaultData:T;
/**
*
*/
private cache:T;
/**
* cache storage
*/
private saveWaiter:Waiter = new Waiter();
/**
* cache storage
*/
public async save():Promise<void> {
// 如果没有开始存储
// 发起一次异步读取
if(this.saveWaiter.state === StorageState.DONE)
setTimeout(() => {
// 获取函数绑定列表
let fnList = this.saveWaiter.done();
wx.setStorage<T>({
key: this.key,
data: this.cache,
success: (data) => {
Logger.log(`数据保存成功! errMsg: ${ data.errMsg }`,
LevelLogLabel.TraceLabel, this.StorageLogLabel);
},
fail: (data) => {
Logger.log(`数据保存失败! errMsg: ${ data.errMsg }`,
LevelLogLabel.FatalLabel, this.StorageLogLabel);
},
complete: () => {
fnList();
}
});
});
return new Promise((r) => {
// 加入等待队列
this.saveWaiter.addWaiter(r);
});
}
/**
*
*/
public async reset() {
this.cache = this.defaultData;
Logger.logMultiple([LevelLogLabel.InfoLabel, this.StorageLogLabel],
`正在重置为默认数据... 数据内容:\n`, this.cache
);
return this.save();
}
/**
* @param defaultData
*/
public constructor(key:string, defaultData:T) {
this.key = key;
this.defaultData = defaultData;
this.StorageLogLabel = new LogLabel(
`Storage:${ this.key }`, colorRadio(34, 230, 258)
);
// 读取数据到缓存
this.cache = wx.getStorageSync<T>(this.key);
// 使用默认值
if(!this.cache) {
Logger.log(`找不到 Storage 数据!`,
LevelLogLabel.InfoLabel, this.StorageLogLabel);
this.reset();
} else {
Logger.logMultiple([LevelLogLabel.InfoLabel, this.StorageLogLabel],
`数据成功从 Storage 读取, 数据内容:\n`, this.cache
);
}
}
/**
*
* @param key
*/
public get<M extends keyof T>(key:M):T[M] {
return this.cache[key];
}
/**
*
* @param key
*/
public async set<M extends keyof T>(key:M, value:T[M]):Promise<void> {
this.cache[key] = value;
return this.save();
}
}
export default Storage;
export { Storage, StorageState, Waiter };

View File

@ -1,5 +1,5 @@
import { Modular, Manager, ILifetime } from "../../core/Module";
import { LevelLogLabel, LifeCycleLogLabel, colorRadio } from "../../core/PresetLogLabel";
import { LevelLogLabel, LifeCycleLogLabel, NormalStyle } from "../../core/PresetLogLabel";
import { LogLabel } from "../../core/LogLabel";
import { Logger } from "../../core/Logger";
@ -41,7 +41,7 @@ implements Partial<ILifetime> {
*
*/
public static readonly StatusBarLabel = new LogLabel(
"StatusBar", colorRadio(255, 230, 222)
"StatusBar", NormalStyle
);
data = {