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: {