From 41519b78e4c576c0eb62eb644e50a662589c61ad Mon Sep 17 00:00:00 2001 From: MrKBear Date: Tue, 21 Dec 2021 07:48:03 +0800 Subject: [PATCH 1/4] Add API Lib --- miniprogram/app.ts | 44 ++++++++++- miniprogram/core/Api.ts | 162 ++++++++++++++++++++++++++++++++++++++++ project.config.json | 2 +- 3 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 miniprogram/core/Api.ts diff --git a/miniprogram/app.ts b/miniprogram/app.ts index f5d5c29..20ce685 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -1,6 +1,6 @@ import { Logger } from "./core/Logger"; import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel"; - +import { API, IParamSetting } from "./core/Api"; import { Storage } from "./core/Storage"; @@ -26,5 +26,47 @@ App({ setTimeout(() => { s.set("be", 12); }, 1000) + + interface ITestApiInput { + name: string, + id: number, + info: { + data: string + } + } + + class TestApi extends API { + + public override key:string = "TestApi"; + + public override params: IParamSetting = { + name: { + tester: "123" + }, + id: { + parse: (i) => ++i, + }, + info: {} + } + + public constructor(data:ITestApiInput) { + super(data); + this.initLabel(); + this.initData(); + } + } + + function testApi() { + let api = new TestApi({ + name: "123", + id: 456, + info: { + data: "abc" + } + }); + console.log(api); + } + + testApi(); } }) \ No newline at end of file diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts new file mode 100644 index 0000000..e3098c8 --- /dev/null +++ b/miniprogram/core/Api.ts @@ -0,0 +1,162 @@ +import { LogLabel } from "./LogLabel"; +import { Logger } from "./Logger"; +import { LevelLogLabel, colorRadio } from "./PresetLogLabel"; + +interface IAnyData { + [x:string]: any +} + +type DeepReadonly = { + readonly [P in keyof T]: DeepReadonly; +}; + +/** + * 请求参数设置 + */ +type IParamSetting = { + [P in keyof T]: { + + /** + * 默认值 + */ + defaultValue?: T[P], + + /** + * 测试 + */ + tester?: RegExp | ((data:T[P]) => boolean) | string, + + /** + * 预解析函数 + */ + parse?: ((data:T[P], key:string, all:DeepReadonly>) => T[P]), + + /** + * 是否为请求头数据 + */ + isHeader?: boolean, + + /** + * 是否为模板 + */ + isTemplate?: boolean + + /** + * 是否可选 + */ + Optional?: boolean + } +} + +/** + * 接口调用 + */ +class API { + + public static defaultLogLabel:LogLabel = new LogLabel( + `API:API`, colorRadio(200, 120, 222) + ); + + /** + * Logger 使用的标签 + */ + private LogLabel:LogLabel = API.defaultLogLabel; + + /** + * Api 唯一 ID + */ + public key:string = "API"; + + /** + * API 需要的参数列表 + */ + public params:IParamSetting = {} as any; + + /** + * API 需要的数据 + */ + public data:Partial; + + /** + * 请求数据 + */ + public requestData:IAnyData = {} as any; + + /** + * 构造函数 + * 注意:data 是不安全的,请传入数据副本 + * @param data API需要的全部数据 + */ + public constructor(data? :Partial) { + this.data = data ?? {}; + } + + /** + * 初始化标签 + */ + public initLabel() { + this.LogLabel = new LogLabel( + `API:${ this.key }`, colorRadio(200, 120, 222) + ); + } + + /** + * 初始化数据 + */ + public initData() { + for(let key in this.params) { + + let data = this.data[key]; + let { defaultValue, Optional, tester, parse } = this.params[key]; + + // 默认值赋予 + if(data === void 0 && defaultValue !== void 0) { + this.data[key] = defaultValue; + } + + // 数据存在测试 + if(data === void 0 && !Optional) { + Logger.log(`数据校验异常: 数据 [${key}] 是必须的,但是并没有接收到!`, + LevelLogLabel.FatalLabel, this.LogLabel); + } + + // 用户自定义测试 + if(data !== void 0 && tester !== void 0) { + let testRes:boolean = false; + + if(tester instanceof RegExp) { + testRes = tester.test(data!); + } else if(typeof tester === "string") { + testRes = tester === data; + } else if(tester instanceof Function) { + testRes = tester(data!); + } else { + Logger.logMultiple( + [LevelLogLabel.FatalLabel, this.LogLabel], + `数据校验异常: [${ key }] 参数存在未知类型的 tester:`, tester + ); + } + + if(!testRes) { + Logger.logMultiple( + [LevelLogLabel.FatalLabel, this.LogLabel], + `数据校验异常: [${ key }] 参数数据未通过自定义的 tester:`, data + ); + } + } + + // 数据预解析 + if(parse !== void 0 && data !== void 0) { + this.data[key] = parse(data!, key, this.data as DeepReadonly>); + } + } + } + + /** + * 收集请求数据 + */ + public collectData() {} +} + +export default API; +export { API, IParamSetting } \ No newline at end of file diff --git a/project.config.json b/project.config.json index bf78da6..ec8c334 100644 --- a/project.config.json +++ b/project.config.json @@ -35,7 +35,7 @@ "outputPath": "" }, "enableEngineNative": false, - "useIsolateContext": true, + "useIsolateContext": false, "userConfirmedBundleSwitch": false, "packNpmManually": false, "packNpmRelationList": [], -- 2.45.2 From 0140136a0bb4dffe6115181cc1b4f18928339d57 Mon Sep 17 00:00:00 2001 From: "ben.qin" Date: Wed, 29 Dec 2021 17:33:27 +0800 Subject: [PATCH 2/4] Add API Moudle! --- miniprogram/app.ts | 53 ---------- miniprogram/core/Api.ts | 120 ++++++++++++++++++----- miniprogram/pages/Timetable/TestCore.ts | 63 ++++++++++++ miniprogram/pages/Timetable/Timetable.ts | 2 + 4 files changed, 163 insertions(+), 75 deletions(-) create mode 100644 miniprogram/pages/Timetable/TestCore.ts diff --git a/miniprogram/app.ts b/miniprogram/app.ts index 20ce685..8c29ae4 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -1,7 +1,5 @@ import { Logger } from "./core/Logger"; import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel"; -import { API, IParamSetting } from "./core/Api"; -import { Storage } from "./core/Storage"; App({ @@ -17,56 +15,5 @@ App({ onLaunch() { Logger.log("小程序启动...", LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLaunchLabel); - - let s = new Storage("test", { - a: new Date(), - be: 2 - }); - - setTimeout(() => { - s.set("be", 12); - }, 1000) - - interface ITestApiInput { - name: string, - id: number, - info: { - data: string - } - } - - class TestApi extends API { - - public override key:string = "TestApi"; - - public override params: IParamSetting = { - name: { - tester: "123" - }, - id: { - parse: (i) => ++i, - }, - info: {} - } - - public constructor(data:ITestApiInput) { - super(data); - this.initLabel(); - this.initData(); - } - } - - function testApi() { - let api = new TestApi({ - name: "123", - id: 456, - info: { - data: "abc" - } - }); - console.log(api); - } - - testApi(); } }) \ No newline at end of file diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts index e3098c8..486d673 100644 --- a/miniprogram/core/Api.ts +++ b/miniprogram/core/Api.ts @@ -6,6 +6,8 @@ interface IAnyData { [x:string]: any } +type IWxRequestOption = WechatMiniprogram.RequestOption; + type DeepReadonly = { readonly [P in keyof T]: DeepReadonly; }; @@ -22,14 +24,26 @@ type IParamSetting = { defaultValue?: T[P], /** - * 测试 + * ### 数据测试 + * 1、支持正则表达式测试 \ + * 2、支持使用 string === string 测试 \ + * 3、支持使用 number === number 测试 \ + * 4、支持使用自定义函数测试 */ - tester?: RegExp | ((data:T[P]) => boolean) | string, + tester?: RegExp | ((data:T[P]) => boolean) | string | number, /** - * 预解析函数 + * ### 预解析函数 + * 1、此函数用来处理该键值 \ + * 2、当返回 undefined 时此键值被遗弃 \ + * 3、返回值时,此键值被覆盖 + * + * @param data 当前给定数据 + * @param key 当前给定数据键值 + * @param all 全部输入数据 + * @returns 解析结果 */ - parse?: ((data:T[P], key:string, all:DeepReadonly>) => T[P]), + parse?: ((data:T[P], key:string, all:DeepReadonly>) => T[P] | undefined), /** * 是否为请求头数据 @@ -53,6 +67,10 @@ type IParamSetting = { */ class API { + public static get baseUrl():string { + return "https://xxx.xxx" + } + public static defaultLogLabel:LogLabel = new LogLabel( `API:API`, colorRadio(200, 120, 222) ); @@ -67,6 +85,11 @@ class API { */ public key:string = "API"; + /** + * API url + */ + public url:string = "/"; + /** * API 需要的参数列表 */ @@ -75,19 +98,29 @@ class API { /** * API 需要的数据 */ - public data:Partial; + public data?:Partial; /** * 请求数据 */ - public requestData:IAnyData = {} as any; + public requestData?:IWxRequestOption; + + /** + * 超时时间 (wx.request) + */ + public timeout?:number; + + /** + * 请求方法 (wx.request) + */ + public method:HTTPMethod = HTTPMethod.GET; /** * 构造函数 * 注意:data 是不安全的,请传入数据副本 * @param data API需要的全部数据 */ - public constructor(data? :Partial) { + public constructor(data?: Partial) { this.data = data ?? {}; } @@ -104,31 +137,38 @@ class API { * 初始化数据 */ public initData() { - for(let key in this.params) { + + if (this.data === void 0) { + Logger.log(`数据初始化异常: 没有输入 [data] 数据!`, + LevelLogLabel.FatalLabel, this.LogLabel); + return; + } + + for (let key in this.params) { let data = this.data[key]; - let { defaultValue, Optional, tester, parse } = this.params[key]; + let { defaultValue, Optional, tester } = this.params[key]; // 默认值赋予 - if(data === void 0 && defaultValue !== void 0) { + if (data === void 0 && defaultValue !== void 0) { this.data[key] = defaultValue; } // 数据存在测试 - if(data === void 0 && !Optional) { + if (data === void 0 && !Optional) { Logger.log(`数据校验异常: 数据 [${key}] 是必须的,但是并没有接收到!`, LevelLogLabel.FatalLabel, this.LogLabel); } // 用户自定义测试 - if(data !== void 0 && tester !== void 0) { + if (data !== void 0 && tester !== void 0) { let testRes:boolean = false; - if(tester instanceof RegExp) { + if (tester instanceof RegExp) { testRes = tester.test(data!); - } else if(typeof tester === "string") { + } else if (typeof tester === "string" || typeof tester === "number") { testRes = tester === data; - } else if(tester instanceof Function) { + } else if (tester instanceof Function) { testRes = tester(data!); } else { Logger.logMultiple( @@ -137,25 +177,61 @@ class API { ); } - if(!testRes) { + if (!testRes) { Logger.logMultiple( [LevelLogLabel.FatalLabel, this.LogLabel], `数据校验异常: [${ key }] 参数数据未通过自定义的 tester:`, data ); } } - - // 数据预解析 - if(parse !== void 0 && data !== void 0) { - this.data[key] = parse(data!, key, this.data as DeepReadonly>); - } } } /** * 收集请求数据 */ - public collectData() {} + public collectData() { + + if (this.data === void 0) { + Logger.log(`收集请求数据异常: 没有输入 [data] 数据!`, + LevelLogLabel.FatalLabel, this.LogLabel); + return; + } + + // 重置请求数据 + const requestData:IWxRequestOption = this.requestData = { + url: API.baseUrl + this.url, + }; + + for (let key in this.params) { + + let data = this.data[key]; + let { isHeader, isTemplate, parse } = this.params[key]; + + // 数据预解析 + if (parse !== void 0) { + data = parse(data!, key, this.data as DeepReadonly>); + } + + // 加载数据 + if (!isTemplate) { + if (isHeader) { + wx.request + } + } + } + } +} + +enum HTTPMethod { + OPTIONS = "OPTIONS", + GET = "GET", + HEAD = "HEAD", + POST = "POST", + PUT = "PUT", + DELETE = "DELETE", + TRACE = "TRACE", + CONNECT = "CONNECT" } export default API; diff --git a/miniprogram/pages/Timetable/TestCore.ts b/miniprogram/pages/Timetable/TestCore.ts new file mode 100644 index 0000000..0cb5ac3 --- /dev/null +++ b/miniprogram/pages/Timetable/TestCore.ts @@ -0,0 +1,63 @@ +import { Modular, Manager, ILifetime } from "../../core/Module"; +import { API, IParamSetting } from "../../core/Api"; +import { Storage } from "../../core/Storage"; + +/** + * 顶部状态栏 + */ +class TestCore extends Modular +implements Partial { + + public onLoad() { + + let s = new Storage("test", { + a: new Date(), + be: 2 + }); + + setTimeout(() => { + s.set("be", 12); + }, 1000) + + interface ITestApiInput { + name: string, + id: number, + info: { + data: string + } + } + + class TestApi extends API { + + public override key:string = "TestApi"; + + public override params: IParamSetting = { + name: { + tester: "123" + }, + id: { + parse: (i) => ++i, + }, + info: {} + } + + public constructor(data:ITestApiInput) { + super(data); + this.initLabel(); + this.initData(); + } + } + + let api = new TestApi({ + name: "123", + id: 456, + info: { + data: "abc" + } + }); + console.log(api); + } +} + +export default TestCore; +export { TestCore }; \ No newline at end of file diff --git a/miniprogram/pages/Timetable/Timetable.ts b/miniprogram/pages/Timetable/Timetable.ts index 82a4874..05c0898 100644 --- a/miniprogram/pages/Timetable/Timetable.ts +++ b/miniprogram/pages/Timetable/Timetable.ts @@ -1,5 +1,6 @@ import { Manager} from "../../core/Module"; import { StatusBar } from "./StatusBar"; +import { TestCore } from "./TestCore"; /** * 此页面使用 Manager 进行模块化管理 @@ -7,4 +8,5 @@ import { StatusBar } from "./StatusBar"; */ Manager.Page((manager)=>{ manager.addModule(StatusBar, "statusBar"); + manager.addModule(TestCore, "testCore"); }) \ No newline at end of file -- 2.45.2 From c3773a3c716df40e667332933d73ec8b24462b2a Mon Sep 17 00:00:00 2001 From: mrkbear Date: Wed, 29 Dec 2021 21:03:10 +0800 Subject: [PATCH 3/4] Add API Module! --- miniprogram/core/Api.ts | 93 +++++++++++++++++++++---- miniprogram/pages/Timetable/TestCore.ts | 10 ++- 2 files changed, 87 insertions(+), 16 deletions(-) diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts index 486d673..0187c89 100644 --- a/miniprogram/core/Api.ts +++ b/miniprogram/core/Api.ts @@ -1,3 +1,4 @@ +import mitt, { Emitter } from "./EventEmitter"; import { LogLabel } from "./LogLabel"; import { Logger } from "./Logger"; import { LevelLogLabel, colorRadio } from "./PresetLogLabel"; @@ -62,13 +63,33 @@ type IParamSetting = { } } +/** + * API 事件 + */ +type IAPIEvent = { + + /** + * 当数据初始化事件 + */ + initData: Partial; + + /** + * 请求数据解析完成后 + */ + parseRequestData: Partial; +} + /** * 接口调用 */ class API { + /** + * 基础 URL + * TODO: 这里可能涉及负载均衡 + */ public static get baseUrl():string { - return "https://xxx.xxx" + return "https://xxx.xxx"; } public static defaultLogLabel:LogLabel = new LogLabel( @@ -80,6 +101,15 @@ class API { */ private LogLabel:LogLabel = API.defaultLogLabel; + /** + * 事件监听器 + */ + private emitter:Emitter>; + + public get on() { return this.emitter.on }; + public get off() { return this.emitter.on }; + public get emit() { return this.emitter.emit }; + /** * Api 唯一 ID */ @@ -105,15 +135,21 @@ class API { */ public requestData?:IWxRequestOption; - /** - * 超时时间 (wx.request) - */ - public timeout?:number; + //#region wx.request - /** - * 请求方法 (wx.request) - */ + public timeout?:number; public method:HTTPMethod = HTTPMethod.GET; + public enableHttp2:boolean = false; + public enableQuic:boolean = false; + public enableCache:boolean = false; + + /** + * 是否自动解析返回的 json + * 对应 wx.request 的 dataType + */ + public jsonParse:boolean = true; + + //#endregion wx.request /** * 构造函数 @@ -122,6 +158,7 @@ class API { */ public constructor(data?: Partial) { this.data = data ?? {}; + this.emitter = mitt>(); } /** @@ -185,6 +222,9 @@ class API { } } } + + // 触发数据初始化事件 + this.emit("initData", this.data); } /** @@ -201,28 +241,51 @@ class API { // 重置请求数据 const requestData:IWxRequestOption = this.requestData = { url: API.baseUrl + this.url, + data: {}, header: {}, + timeout: this.timeout, + method: this.method, + dataType: this.jsonParse ? "json" : undefined, + enableHttp2: this.enableHttp2, + enableQuic: this.enableQuic, + enableCache: this.enableCache }; + // 数据解析 + for (let key in this.params) { + + let data = this.data[key]; + let { parse } = this.params[key]; + + // 数据预解析 + if (parse !== void 0) { + this.data[key] = parse(data!, key, this.data as DeepReadonly>); + } + } + + // 触发数据解析 + this.emit("parseRequestData", this.data); + + // 数据收集 for (let key in this.params) { let data = this.data[key]; - let { isHeader, isTemplate, parse } = this.params[key]; - - // 数据预解析 - if (parse !== void 0) { - data = parse(data!, key, this.data as DeepReadonly>); - } + let { isHeader, isTemplate } = this.params[key]; // 加载数据 if (!isTemplate) { if (isHeader) { - wx.request + requestData.header![key] = data; + } else { + (requestData.data as IAnyData)[key] = data; } } } } } +/** + * HTTP 请求类型 + */ enum HTTPMethod { OPTIONS = "OPTIONS", GET = "GET", diff --git a/miniprogram/pages/Timetable/TestCore.ts b/miniprogram/pages/Timetable/TestCore.ts index 0cb5ac3..9e3b51e 100644 --- a/miniprogram/pages/Timetable/TestCore.ts +++ b/miniprogram/pages/Timetable/TestCore.ts @@ -33,7 +33,8 @@ implements Partial { public override params: IParamSetting = { name: { - tester: "123" + tester: "123", + isHeader: true }, id: { parse: (i) => ++i, @@ -43,8 +44,15 @@ implements Partial { public constructor(data:ITestApiInput) { super(data); + this.on("initData", (data) => { + console.log("initData", data) + }) + this.on("parseRequestData", (data) => { + console.log("parseRequestData", data) + }) this.initLabel(); this.initData(); + this.collectData(); } } -- 2.45.2 From 14b03af5843fd0c5f16b573d6158960e5f54c58e Mon Sep 17 00:00:00 2001 From: "ben.qin" Date: Thu, 30 Dec 2021 16:14:21 +0800 Subject: [PATCH 4/4] Modify EventEmitter to class style --- miniprogram/app.ts | 14 +- miniprogram/core/Api.ts | 52 ++++---- miniprogram/core/EventEmitter.ts | 164 +++++++++++------------- miniprogram/core/Module.ts | 35 +---- miniprogram/pages/Timetable/TestCore.ts | 19 +-- 5 files changed, 128 insertions(+), 156 deletions(-) diff --git a/miniprogram/app.ts b/miniprogram/app.ts index 8c29ae4..004bd19 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -1,13 +1,23 @@ +import { IAppAPIParam } from "./core/Api"; import { Logger } from "./core/Logger"; import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel"; -App({ +App({ + + /** + * API 模块需要的全局数据 + * 参见 "/core/Api" + */ + api: { + nextId: 1, + pool: [] + }, /** * 存储缓存键值 */ - storageCache: new Set(), + // storageCache: new Set(), /** * 小程序加载时 diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts index 0187c89..041df1b 100644 --- a/miniprogram/core/Api.ts +++ b/miniprogram/core/Api.ts @@ -1,8 +1,23 @@ -import mitt, { Emitter } from "./EventEmitter"; +import { EventEmitter } from "./EventEmitter"; import { LogLabel } from "./LogLabel"; import { Logger } from "./Logger"; import { LevelLogLabel, colorRadio } from "./PresetLogLabel"; +interface IAppAPIParam { + api: { + + /** + * API 编号 + */ + nextId: number; + + /** + * 请求池 + */ + pool: API[]; + } +} + interface IAnyData { [x:string]: any } @@ -82,7 +97,7 @@ type IAPIEvent = { /** * 接口调用 */ -class API { +class API extends EventEmitter> { /** * 基础 URL @@ -101,15 +116,6 @@ class API { */ private LogLabel:LogLabel = API.defaultLogLabel; - /** - * 事件监听器 - */ - private emitter:Emitter>; - - public get on() { return this.emitter.on }; - public get off() { return this.emitter.on }; - public get emit() { return this.emitter.emit }; - /** * Api 唯一 ID */ @@ -151,16 +157,6 @@ class API { //#endregion wx.request - /** - * 构造函数 - * 注意:data 是不安全的,请传入数据副本 - * @param data API需要的全部数据 - */ - public constructor(data?: Partial) { - this.data = data ?? {}; - this.emitter = mitt>(); - } - /** * 初始化标签 */ @@ -172,8 +168,12 @@ class API { /** * 初始化数据 + * 注意:data 是不安全的,请传入数据副本 + * @param data API需要的全部数据 */ - public initData() { + public param(data?: Partial) { + + this.data = data; if (this.data === void 0) { Logger.log(`数据初始化异常: 没有输入 [data] 数据!`, @@ -225,12 +225,6 @@ class API { // 触发数据初始化事件 this.emit("initData", this.data); - } - - /** - * 收集请求数据 - */ - public collectData() { if (this.data === void 0) { Logger.log(`收集请求数据异常: 没有输入 [data] 数据!`, @@ -298,4 +292,4 @@ enum HTTPMethod { } export default API; -export { API, IParamSetting } \ No newline at end of file +export { API, IParamSetting, IAppAPIParam, HTTPMethod } \ No newline at end of file diff --git a/miniprogram/core/EventEmitter.ts b/miniprogram/core/EventEmitter.ts index 2dc3cd7..34fb56d 100644 --- a/miniprogram/core/EventEmitter.ts +++ b/miniprogram/core/EventEmitter.ts @@ -18,102 +18,92 @@ export type EventHandlerMap> = Map< EventHandlerList | WildCardEventHandlerList >; -export interface Emitter> { - all: EventHandlerMap; +type GenericEventHandler> = + | Handler + | WildcardHandler; + +export class EventEmitter> { + + /** + * A Map of event names to registered handler functions. + */ + public all: EventHandlerMap; + + public constructor() { + this.all = new Map(); + } on(type: Key, handler: Handler): void; on(type: '*', handler: WildcardHandler): void; + /** + * Register an event handler for the given type. + * @param {string|symbol} type Type of event to listen for, or `'*'` for all events + * @param {Function} handler Function to call in response to given event + * @memberOf mitt + */ + public on(type: Key, handler: GenericEventHandler) { + const handlers: Array> | undefined = this.all!.get(type); + if (handlers) { + handlers.push(handler); + } + else { + this.all!.set(type, [handler] as EventHandlerList); + } + } + off(type: Key, handler?: Handler): void; off(type: '*', handler: WildcardHandler): void; - emit(type: Key, event: Events[Key]): void; - emit(type: undefined extends Events[Key] ? Key : never): void; -} - -/** - * Mitt: Tiny (~200b) functional event emitter / pubsub. - * @name mitt - * @returns {Mitt} - */ -export default function mitt>( - all?: EventHandlerMap -): Emitter { - type GenericEventHandler = - | Handler - | WildcardHandler; - all = all || new Map(); - - return { - - /** - * A Map of event names to registered handler functions. - */ - all, - - /** - * Register an event handler for the given type. - * @param {string|symbol} type Type of event to listen for, or `'*'` for all events - * @param {Function} handler Function to call in response to given event - * @memberOf mitt - */ - on(type: Key, handler: GenericEventHandler) { - const handlers: Array | undefined = all!.get(type); - if (handlers) { - handlers.push(handler); + /** + * Remove an event handler for the given type. + * If `handler` is omitted, all handlers of the given type are removed. + * @param {string|symbol} type Type of event to unregister `handler` from, or `'*'` + * @param {Function} [handler] Handler function to remove + * @memberOf mitt + */ + public off(type: Key, handler?: GenericEventHandler) { + const handlers: Array> | undefined = this.all!.get(type); + if (handlers) { + if (handler) { + handlers.splice(handlers.indexOf(handler) >>> 0, 1); } else { - all!.set(type, [handler] as EventHandlerList); - } - }, - - /** - * Remove an event handler for the given type. - * If `handler` is omitted, all handlers of the given type are removed. - * @param {string|symbol} type Type of event to unregister `handler` from, or `'*'` - * @param {Function} [handler] Handler function to remove - * @memberOf mitt - */ - off(type: Key, handler?: GenericEventHandler) { - const handlers: Array | undefined = all!.get(type); - if (handlers) { - if (handler) { - handlers.splice(handlers.indexOf(handler) >>> 0, 1); - } - else { - all!.set(type, []); - } - } - }, - - /** - * Invoke all handlers for the given type. - * If present, `'*'` handlers are invoked after type-matched handlers. - * - * Note: Manually firing '*' handlers is not supported. - * - * @param {string|symbol} type The event type to invoke - * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler - * @memberOf mitt - */ - emit(type: Key, evt?: Events[Key]) { - let handlers = all!.get(type); - if (handlers) { - (handlers as EventHandlerList) - .slice() - .map((handler) => { - handler(evt!); - }); - } - - handlers = all!.get('*'); - if (handlers) { - (handlers as WildCardEventHandlerList) - .slice() - .map((handler) => { - handler(type, evt!); - }); + this.all!.set(type, []); } } - }; + } + + emit(type: Key, event: Events[Key]): void; + emit(type: undefined extends Events[Key] ? Key : never): void; + + /** + * Invoke all handlers for the given type. + * If present, `'*'` handlers are invoked after type-matched handlers. + * + * Note: Manually firing '*' handlers is not supported. + * + * @param {string|symbol} type The event type to invoke + * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler + * @memberOf mitt + */ + emit(type: Key, evt?: Events[Key]) { + let handlers = this.all!.get(type); + if (handlers) { + (handlers as EventHandlerList) + .slice() + .map((handler) => { + handler(evt!); + }); + } + + handlers = this.all!.get('*'); + if (handlers) { + (handlers as WildCardEventHandlerList) + .slice() + .map((handler) => { + handler(type, evt!); + }); + } + } } \ No newline at end of file diff --git a/miniprogram/core/Module.ts b/miniprogram/core/Module.ts index 6fdf8c2..e05192e 100644 --- a/miniprogram/core/Module.ts +++ b/miniprogram/core/Module.ts @@ -1,4 +1,4 @@ -import mitt, { Emitter, EventHandlerMap, EventType, Handler, WildcardHandler } from "./EventEmitter"; +import { EventEmitter, EventType } from "./EventEmitter"; import { LogLabel, LogStyle } from "./LogLabel"; import { Logger } from "./Logger"; import { LevelLogLabel } from "./PresetLogLabel"; @@ -64,7 +64,8 @@ class Modular< DEP extends Depends = Depends, E extends Record = Record, TD extends IAnyTypeObject = IAnyTypeObject> -implements WXContext, Emitter { +extends EventEmitter +implements WXContext { // [x:string]: any; @@ -121,6 +122,8 @@ implements WXContext, Emitter { */ public constructor(manager:M, nameSpace:string, depend?: DEP) { + super(); + // 保存微信上下文 this.manager = manager; @@ -131,34 +134,6 @@ implements WXContext, Emitter { this.functionList = new Set(); this.paramList = new Set(); this.nameSpace = nameSpace; - - this.emitter = mitt(); - - } - - /** - * 内部事件控制器 - */ - private emitter:Emitter; - - public get all():EventHandlerMap { return this.emitter.all }; - - on(type: Key, handler: Handler): void; - on(type: "*", handler: WildcardHandler): void; - on(type: any, handler: any): void { - return this.emitter.on(type, handler); - } - - off(type: Key, handler?: Handler): void; - off(type: "*", handler: WildcardHandler): void; - off(type: any, handler?: any): void { - return this.emitter.off(type, handler); - } - - emit(type: Key, event: E[Key]): void; - emit(type: undefined extends E[Key] ? Key : never): void; - emit(type: any, event?: any): void { - return this.emitter.emit(type, event); } public setData(data:Partial, callback?: () => void):void { diff --git a/miniprogram/pages/Timetable/TestCore.ts b/miniprogram/pages/Timetable/TestCore.ts index 9e3b51e..2ebf306 100644 --- a/miniprogram/pages/Timetable/TestCore.ts +++ b/miniprogram/pages/Timetable/TestCore.ts @@ -42,21 +42,24 @@ implements Partial { info: {} } - public constructor(data:ITestApiInput) { - super(data); - this.on("initData", (data) => { - console.log("initData", data) + public constructor() { + super(); + this.initLabel(); + + this.emit("initData", {}) + + this.on("initData", (d) => { + }) + this.on("parseRequestData", (data) => { console.log("parseRequestData", data) }) - this.initLabel(); - this.initData(); - this.collectData(); } } - let api = new TestApi({ + let api = new TestApi(); + api.param({ name: "123", id: 456, info: { -- 2.45.2