Add EventEmitter into Module, Optimize Manger so that it can load data #8

Merged
MrKBear merged 4 commits from dev-mrkbear into master 2021-12-01 16:31:55 +08:00
3 changed files with 137 additions and 50 deletions
Showing only changes of commit 55d52f446f - Show all commits

View File

@ -1,4 +1,7 @@
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 };
@ -324,11 +327,10 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
*
* @param key
*/
public creatHooks(key:keyof ILifetime):ILifetime[keyof ILifetime] {
return (...arg: any[]) => {
public creatHooks(key:keyof ILifetime):(...arg: any[]) => Promise<any> {
return async (...arg: any[]) => {
let hooks:Promise<any>[] = [];
let simple:any;
for(let i = 0; i < this.modules.length; i++) {
@ -339,27 +341,30 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
if (res instanceof Promise) {
hooks.push(res);
} else {
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(hooks.length === 1) return simple;
if (
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);
}
@ -377,13 +382,81 @@ class Manager<WXC extends AnyWXContext = AnyWXContext> {
/**
*
* @param query onLoad
*/
public loadAllModule(query:Record<string, string | undefined>) {
// 创建全部钩子
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);
// 打印每个模块的键值对使用情况
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;
}
/**
*
*/
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}

View File

@ -2,53 +2,63 @@ import { Logger } from "../../core/Logger";
import { LevelLogLabel, LifeCycleLogLabel } from "../../core/PresetLogLabel";
import { Manager, Modular, AnyWXContext, ILifetime } from "../../core/Module";
Page({
let manager;
/**
*
*/
onLoad: async function (query) {
// Page({
this;
// /**
// * 课程表页面加载
// */
// onLoad: async function (query) {
let manager = new Manager(this);
let m1 = manager.addModule(M1, "m1");
let m2 = manager.addModule(M2, "m2", {m1});
// manager = new Manager(this);
// console.log(manager);
manager.loadAllModule(query);
this.setData;
// // this.setData;
Logger.log("课程表 (Timetable) 页面加载...",
LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLoadLabel);
// // Logger.log("课程表 (Timetable) 页面加载...",
// // 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> {
data = {
a:1,
b:{}
}
public onLoad(){
}
@ -60,7 +70,11 @@ class M1<M extends Manager> extends Modular<M, {}> implements Partial<ILifetime>
}
class M2<M extends Manager> extends Modular<M, {m1:M1<M>}> {
data = {
a: 1
}
public onLoad() {
this.setData({a:1});
this.depends?.m1.on("lll", (e)=>{

View File

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