Optimize Manger so that it can load data

This commit is contained in:
MrKBear 2021-12-01 16:14:10 +08:00
parent da658cb371
commit 55d52f446f
3 changed files with 137 additions and 50 deletions

View File

@ -1,4 +1,7 @@
import mitt, { Emitter, EventHandlerMap, EventType, Handler, WildcardHandler } from "./EventEmitter"; import mitt, { Emitter, EventHandlerMap, EventType, Handler, WildcardHandler } from "./EventEmitter";
import { LogLabel, LogStyle } from "./LogLabel";
import { Logger } from "./Logger";
import { LevelLogLabel } from "./PresetLogLabel";
/** /**
* *
@ -90,17 +93,17 @@ implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
/** /**
* 使 * 使
*/ */
private functionList:Set<string>; public functionList:Set<string>;
/** /**
* 使 * 使
*/ */
private paramList:Set<string>; public paramList:Set<string>;
/** /**
* *
*/ */
private nameSpace:string; public nameSpace:string;
// 映射主上下文属性 // 映射主上下文属性
public get is():string { return this.context.is }; public get is():string { return this.context.is };
@ -324,11 +327,10 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
* *
* @param key * @param key
*/ */
public creatHooks(key:keyof ILifetime):ILifetime[keyof ILifetime] { public creatHooks(key:keyof ILifetime):(...arg: any[]) => Promise<any> {
return (...arg: any[]) => { return async (...arg: any[]) => {
let hooks:Promise<any>[] = []; let hooks:Promise<any>[] = [];
let simple:any;
for(let i = 0; i < this.modules.length; i++) { for(let i = 0; i < this.modules.length; i++) {
@ -339,27 +341,30 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
if (res instanceof Promise) { if (res instanceof Promise) {
hooks.push(res); hooks.push(res);
} else { } else {
hooks.push(Promise.resolve(res)); hooks.push(Promise.resolve(res));
} }
if (
key === "onShareAppMessage" ||
key === "onShareTimeline" ||
key === "onAddToFavorites"
) {
// 如果返回值有特殊含义在处理时进行 MinIn
simple = Object.assign({}, simple, res);
} else {
simple = res;
}
} }
if(hooks.length === 0) return; if (
if(hooks.length === 1) return simple; key === "onShareAppMessage" ||
key === "onShareTimeline" ||
key === "onAddToFavorites"
) {
// 如果返回值有特殊含义在处理时进行 MinIn
return Promise.all(hooks).then((res) => {
let simple:IAnyTypeObject = {};
for(let i = 0; i < res.length; i++) {
simple = Object.assign({}, simple, res);
}
return Promise.resolve(simple);
})
}
return Promise.all(hooks); return Promise.all(hooks);
} }
@ -377,13 +382,81 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
/** /**
* *
* @param query onLoad
*/ */
public loadAllModule(query:Record<string, string | undefined>) { public loadAllModule(query:Record<string, string | undefined>) {
// 创建全部钩子
this.creatAllHooks(); this.creatAllHooks();
// 加载全部模组数据
for(let i = 0; i < this.modules.length; i++) {
if(this.modules[i].data)
for(let key in this.modules[i].data) {
if(this.context.data === void 0) this.context.data = {};
if(this.modules[i].data !== void 0) {
this.context.data[`${ this.modules[i].nameSpace }$${ key }`] =
( this.modules[i].data as IAnyTypeObject )[key];
// this.modules[i]
}
}
}
// 将全部数据发布到视图层
if(this.context.data !== void 0)
this.context.setData(this.context.data);
// 调用全部模块的 onLoad 周期
let res = this.creatHooks("onLoad")(query as any); let res = this.creatHooks("onLoad")(query as any);
// 打印每个模块的键值对使用情况
for(let i = 0; i < this.modules.length; i++) {
let data:string[] = [];
let func:string[] = [];
for(let key of this.modules[i].paramList) {
data.push(`[${ key }]`);
}
for(let key of this.modules[i].functionList) {
func.push(`[${ key }]`);
}
let log:string = `模块 [${ this.modules[i].nameSpace }] 加载:\n`;
log += `Using Props: ${ data.join(", ") }\n`;
log += `Using Function: ${ func.join(", ") }\n`;
Logger.log(log, LevelLogLabel.TraceLabel, Manager.AddModuleLabel);
}
return res; return res;
} }
/**
*
*/
public static readonly AddModuleLabel = new LogLabel("addModule",
new LogStyle().setBorder("4px", `1px solid #8600FF`)
.setColor("#FF00FF", "rgba(54, 0, 255, .2)").setBlank("0 5px")
)
/**
* Manager
* @param fn
*/
public static Page(fn:(manager:Manager<AnyWXContext>) => void) {
Page({
async onLoad(query) {
let manager = new Manager(this);
fn(manager);
manager.loadAllModule(query);
}
})
}
} }
export { Manager, Modular, AnyWXContext, WXContext, ILifetime} export { Manager, Modular, AnyWXContext, WXContext, ILifetime}

View File

@ -2,53 +2,63 @@ import { Logger } from "../../core/Logger";
import { LevelLogLabel, LifeCycleLogLabel } from "../../core/PresetLogLabel"; import { LevelLogLabel, LifeCycleLogLabel } from "../../core/PresetLogLabel";
import { Manager, Modular, AnyWXContext, ILifetime } from "../../core/Module"; import { Manager, Modular, AnyWXContext, ILifetime } from "../../core/Module";
Page({ let manager;
/** // Page({
*
*/
onLoad: async function (query) {
this; // /**
// * 课程表页面加载
// */
// onLoad: async function (query) {
let manager = new Manager(this); // manager = new Manager(this);
let m1 = manager.addModule(M1, "m1"); // console.log(manager);
let m2 = manager.addModule(M2, "m2", {m1});
manager.loadAllModule(query);
this.setData; // // this.setData;
Logger.log("课程表 (Timetable) 页面加载...", // // Logger.log("课程表 (Timetable) 页面加载...",
LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLoadLabel); // // LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLoadLabel);
let systemInfo = wx.getSystemInfoSync(); // // let systemInfo = wx.getSystemInfoSync();
//状态栏高度 // // //状态栏高度
let statusBarHeight = Number(systemInfo.statusBarHeight); // // let statusBarHeight = Number(systemInfo.statusBarHeight);
let menu = wx.getMenuButtonBoundingClientRect() // // let menu = wx.getMenuButtonBoundingClientRect()
//导航栏高度 // // //导航栏高度
let navBarHeight = menu.height + (menu.top - statusBarHeight) * 2 // // let navBarHeight = menu.height + (menu.top - statusBarHeight) * 2
//状态栏加导航栏高度 // // //状态栏加导航栏高度
let navStatusBarHeight = statusBarHeight + menu.height + (menu.top - statusBarHeight) * 2 // // let navStatusBarHeight = statusBarHeight + menu.height + (menu.top - statusBarHeight) * 2
console.log('状态栏高度',statusBarHeight) // // console.log('状态栏高度',statusBarHeight)
console.log('导航栏高度',navBarHeight) // // console.log('导航栏高度',navBarHeight)
console.log('状态栏加导航栏高度',navStatusBarHeight) // // console.log('状态栏加导航栏高度',navStatusBarHeight)
this.setData({barh: navStatusBarHeight}); // // this.setData({barh: navStatusBarHeight});
} // }
// })
Manager.Page((manager)=>{
let m1 = manager.addModule(M1, "m1");
let m2 = manager.addModule(M2, "m2", {m1});
}) })
class M1<M extends Manager> extends Modular<M, {}> implements Partial<ILifetime> { class M1<M extends Manager> extends Modular<M, {}> implements Partial<ILifetime> {
data = {
a:1,
b:{}
}
public onLoad(){ public onLoad(){
} }
@ -61,6 +71,10 @@ class M1<M extends Manager> extends Modular<M, {}> implements Partial<ILifetime>
class M2<M extends Manager> extends Modular<M, {m1:M1<M>}> { class M2<M extends Manager> extends Modular<M, {m1:M1<M>}> {
data = {
a: 1
}
public onLoad() { public onLoad() {
this.setData({a:1}); this.setData({a:1});
this.depends?.m1.on("lll", (e)=>{ this.depends?.m1.on("lll", (e)=>{

View File

@ -1,3 +1,3 @@
<view class="status-bar" style="height:{{barh}}px"></view> <view class="status-bar" style="height:{{barh}}px"></view>
<text>pages/Timetable/Timetable.wxml</text> <text>pages/Timetable/Timetable.wxml{{m1$a}}</text>