From 55d52f446faef9315c3c0741b88df70e92116dd4 Mon Sep 17 00:00:00 2001 From: MrKBear Date: Wed, 1 Dec 2021 16:14:10 +0800 Subject: [PATCH] Optimize Manger so that it can load data --- miniprogram/core/Module.ts | 117 +++++++++++++++++---- miniprogram/pages/Timetable/Timetable.ts | 68 +++++++----- miniprogram/pages/Timetable/Timetable.wxml | 2 +- 3 files changed, 137 insertions(+), 50 deletions(-) diff --git a/miniprogram/core/Module.ts b/miniprogram/core/Module.ts index 76ee33e..5d60c3a 100644 --- a/miniprogram/core/Module.ts +++ b/miniprogram/core/Module.ts @@ -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, Emitter { /** * 模组使用的函数列表 */ - private functionList:Set; + public functionList:Set; /** * 函数使用的参数列表 */ - private paramList:Set; + public paramList:Set; /** * 命名空间 */ - private nameSpace:string; + public nameSpace:string; // 映射主上下文属性 public get is():string { return this.context.is }; @@ -324,11 +327,10 @@ class Manager { * 创建指定生命周期的钩子 * @param key 生命周期键值 */ - public creatHooks(key:keyof ILifetime):ILifetime[keyof ILifetime] { - return (...arg: any[]) => { + public creatHooks(key:keyof ILifetime):(...arg: any[]) => Promise { + return async (...arg: any[]) => { let hooks:Promise[] = []; - let simple:any; for(let i = 0; i < this.modules.length; i++) { @@ -339,27 +341,30 @@ class Manager { 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 { /** * 加载全部的模块 + * @param query onLoad 接收的参数 */ public loadAllModule(query:Record) { + + // 创建全部钩子 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) => void) { + Page({ + async onLoad(query) { + let manager = new Manager(this); + fn(manager); + manager.loadAllModule(query); + } + }) + } + } export { Manager, Modular, AnyWXContext, WXContext, ILifetime} \ No newline at end of file diff --git a/miniprogram/pages/Timetable/Timetable.ts b/miniprogram/pages/Timetable/Timetable.ts index 163721c..d39adb2 100644 --- a/miniprogram/pages/Timetable/Timetable.ts +++ b/miniprogram/pages/Timetable/Timetable.ts @@ -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 extends Modular implements Partial { + data = { + a:1, + b:{} + } + public onLoad(){ } @@ -60,7 +70,11 @@ class M1 extends Modular implements Partial } class M2 extends Modular}> { - + + data = { + a: 1 + } + public onLoad() { this.setData({a:1}); this.depends?.m1.on("lll", (e)=>{ diff --git a/miniprogram/pages/Timetable/Timetable.wxml b/miniprogram/pages/Timetable/Timetable.wxml index 4720305..94c4bb7 100644 --- a/miniprogram/pages/Timetable/Timetable.wxml +++ b/miniprogram/pages/Timetable/Timetable.wxml @@ -1,3 +1,3 @@ -pages/Timetable/Timetable.wxml +pages/Timetable/Timetable.wxml{{m1$a}}