Add API Module #12

Merged
MrKBear merged 4 commits from dev-mrkbear into master 2021-12-30 16:18:24 +08:00
5 changed files with 128 additions and 156 deletions
Showing only changes of commit 14b03af584 - Show all commits

View File

@ -1,13 +1,23 @@
import { IAppAPIParam } from "./core/Api";
import { Logger } from "./core/Logger";
import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel";
App({
App<IAppAPIParam>({
/**
* API
* "/core/Api"
*/
api: {
nextId: 1,
pool: []
},
/**
*
*/
storageCache: new Set<string>(),
// storageCache: new Set<string>(),
/**
*

View File

@ -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<IAnyData, IAnyData>[];
}
}
interface IAnyData {
[x:string]: any
}
@ -82,7 +97,7 @@ type IAPIEvent<I extends IAnyData, O extends IAnyData> = {
/**
*
*/
class API<I extends IAnyData, O extends IAnyData> {
class API<I extends IAnyData, O extends IAnyData> extends EventEmitter<IAPIEvent<I, O>> {
/**
* URL
@ -101,15 +116,6 @@ class API<I extends IAnyData, O extends IAnyData> {
*/
private LogLabel:LogLabel = API.defaultLogLabel;
/**
*
*/
private emitter:Emitter<IAPIEvent<I, O>>;
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<I extends IAnyData, O extends IAnyData> {
//#endregion wx.request
/**
*
* data
* @param data API需要的全部数据
*/
public constructor(data?: Partial<I>) {
this.data = data ?? {};
this.emitter = mitt<IAPIEvent<I, O>>();
}
/**
*
*/
@ -172,8 +168,12 @@ class API<I extends IAnyData, O extends IAnyData> {
/**
*
* data
* @param data API需要的全部数据
*/
public initData() {
public param(data?: Partial<I>) {
this.data = data;
if (this.data === void 0) {
Logger.log(`数据初始化异常: 没有输入 [data] 数据!`,
@ -225,12 +225,6 @@ class API<I extends IAnyData, O extends IAnyData> {
// 触发数据初始化事件
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 }
export { API, IParamSetting, IAppAPIParam, HTTPMethod }

View File

@ -18,38 +18,23 @@ export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
>;
export interface Emitter<Events extends Record<EventType, unknown>> {
all: EventHandlerMap<Events>;
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
on(type: '*', handler: WildcardHandler<Events>): void;
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
off(type: '*', handler: WildcardHandler<Events>): void;
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
}
/**
* Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
*/
export default function mitt<Events extends Record<EventType, unknown>>(
all?: EventHandlerMap<Events>
): Emitter<Events> {
type GenericEventHandler =
type GenericEventHandler<Events extends Record<EventType, unknown>> =
| Handler<Events[keyof Events]>
| WildcardHandler<Events>;
all = all || new Map();
return {
export class EventEmitter<Events extends Record<EventType, unknown>> {
/**
* A Map of event names to registered handler functions.
*/
all,
public all: EventHandlerMap<Events>;
public constructor() {
this.all = new Map();
}
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
on(type: '*', handler: WildcardHandler<Events>): void;
/**
* Register an event handler for the given type.
@ -57,15 +42,18 @@ export default function mitt<Events extends Record<EventType, unknown>>(
* @param {Function} handler Function to call in response to given event
* @memberOf mitt
*/
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
public on<Key extends keyof Events>(type: Key, handler: GenericEventHandler<Events>) {
const handlers: Array<GenericEventHandler<Events>> | undefined = this.all!.get(type);
if (handlers) {
handlers.push(handler);
}
else {
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
this.all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
}
},
}
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
off(type: '*', handler: WildcardHandler<Events>): void;
/**
* Remove an event handler for the given type.
@ -74,17 +62,20 @@ export default function mitt<Events extends Record<EventType, unknown>>(
* @param {Function} [handler] Handler function to remove
* @memberOf mitt
*/
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
public off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler<Events>) {
const handlers: Array<GenericEventHandler<Events>> | undefined = this.all!.get(type);
if (handlers) {
if (handler) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
}
else {
all!.set(type, []);
this.all!.set(type, []);
}
}
},
}
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
/**
* Invoke all handlers for the given type.
@ -97,7 +88,7 @@ export default function mitt<Events extends Record<EventType, unknown>>(
* @memberOf mitt
*/
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
let handlers = all!.get(type);
let handlers = this.all!.get(type);
if (handlers) {
(handlers as EventHandlerList<Events[keyof Events]>)
.slice()
@ -106,7 +97,7 @@ export default function mitt<Events extends Record<EventType, unknown>>(
});
}
handlers = all!.get('*');
handlers = this.all!.get('*');
if (handlers) {
(handlers as WildCardEventHandlerList<Events>)
.slice()
@ -115,5 +106,4 @@ export default function mitt<Events extends Record<EventType, unknown>>(
});
}
}
};
}

View File

@ -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<M> = Depends<M>,
E extends Record<EventType, unknown> = Record<EventType, unknown>,
TD extends IAnyTypeObject = IAnyTypeObject>
implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
extends EventEmitter<E>
implements WXContext<TD, IAnyTypeObject> {
// [x:string]: any;
@ -121,6 +122,8 @@ implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
*/
public constructor(manager:M, nameSpace:string, depend?: DEP) {
super();
// 保存微信上下文
this.manager = manager;
@ -131,34 +134,6 @@ implements WXContext<TD, IAnyTypeObject>, Emitter<E> {
this.functionList = new Set<string>();
this.paramList = new Set<string>();
this.nameSpace = nameSpace;
this.emitter = mitt<E>();
}
/**
*
*/
private emitter:Emitter<E>;
public get all():EventHandlerMap<E> { return this.emitter.all };
on<Key extends keyof E>(type: Key, handler: Handler<E[Key]>): void;
on(type: "*", handler: WildcardHandler<E>): void;
on(type: any, handler: any): void {
return this.emitter.on(type, handler);
}
off<Key extends keyof E>(type: Key, handler?: Handler<E[Key]>): void;
off(type: "*", handler: WildcardHandler<E>): void;
off(type: any, handler?: any): void {
return this.emitter.off(type, handler);
}
emit<Key extends keyof E>(type: Key, event: E[Key]): void;
emit<Key extends keyof E>(type: undefined extends E[Key] ? Key : never): void;
emit(type: any, event?: any): void {
return this.emitter.emit(type, event);
}
public setData(data:Partial<TD>, callback?: () => void):void {

View File

@ -42,21 +42,24 @@ implements Partial<ILifetime> {
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: {