Extend core API(#24) #33

Merged
MrKBear merged 2 commits from dev-mrkbear into master 2022-01-12 12:00:17 +08:00
2 changed files with 76 additions and 9 deletions
Showing only changes of commit 478eafa4a3 - Show all commits

View File

@ -1,4 +1,4 @@
import { Emitter } from "./Emitter"; import { Emitter, EventType } from "./Emitter";
import { API_FAILED_SHOW_MESSAGE } from "./Config"; import { API_FAILED_SHOW_MESSAGE } from "./Config";
import { Logger, LogLabel, LevelLogLabel, colorRadio, StatusLabel } from "./Logger"; import { Logger, LogLabel, LevelLogLabel, colorRadio, StatusLabel } from "./Logger";
interface IAppAPIParam { interface IAppAPIParam {
@ -117,16 +117,57 @@ type IAPIEvent<I extends IAnyData, O extends IAnyData> = {
} }
/** /**
* *
*/ */
class API<I extends IAnyData, O extends IAnyData> extends Emitter<IAPIEvent<I, O>> { type IAPIResultEvent<O extends IAnyData, U extends IAnyData> = {
/**
*
*/
ok: O,
/**
*
*
*/
no: { message: string } & U,
/**
*
*
*/
done: { message: string, data: O } & U
}
/**
* API
* @template I API
* @template O API
* @template E API
* @template U
*/
class API<
I extends IAnyData = IAnyData,
O extends IAnyData = IAnyData,
E extends Record<EventType, any> = Record<EventType, any>,
U extends IAnyData = IAnyData
> extends Emitter <
{
// 这个复杂的泛型是为了 MixIn 用户自定义事件类型
// 懂得如何使用就可以了
// 不要试图去理解下面这三行代码,真正的恶魔在等着你
[P in (keyof (IAPIEvent<I, O> & IAPIResultEvent<O, U>) | keyof E)] :
P extends keyof IAPIEvent<I, O> ? IAPIEvent<I, O>[P] :
P extends keyof IAPIResultEvent<O, U> ? IAPIResultEvent<O, U>[P] : E[P]
}
> {
/** /**
* URL * URL
* TODO: 这里可能涉及负载均衡 * TODO: 这里可能涉及负载均衡
*/ */
public static get baseUrl():string { public static get baseUrl():string {
return "https://blog.mrkbear.com"; return "https://jwc.nogg.cn";
} }
public static defaultLogLabel:LogLabel = new LogLabel( public static defaultLogLabel:LogLabel = new LogLabel(
@ -367,7 +408,7 @@ class API<I extends IAnyData, O extends IAnyData> extends Emitter<IAPIEvent<I, O
// 判断 API 是否相似 // 判断 API 是否相似
app.api.pool.forEach((api) => { app.api.pool.forEach((api) => {
if (api === this) return; if ((api as API | this) === this) return;
if (!api.requestData) return; if (!api.requestData) return;
if (api.requestData!.url !== this.requestData!.url) return; if (api.requestData!.url !== this.requestData!.url) return;
if (api.requestData!.method !== this.requestData!.method) return; if (api.requestData!.method !== this.requestData!.method) return;
@ -472,9 +513,9 @@ class API<I extends IAnyData, O extends IAnyData> extends Emitter<IAPIEvent<I, O
/** /**
* *
*/ */
public wait(): Promise<IRespondData<O>>; public waitRequest(): Promise<IRespondData<O>>;
public wait(callBack?: ICallBack<O>): this; public waitRequest(callBack: ICallBack<O>): this;
public wait(callBack?: ICallBack<O>): Promise<IRespondData<O>> | this { public waitRequest(callBack?: ICallBack<O>): Promise<IRespondData<O>> | this {
// 存在 callback 使用传统回调 // 存在 callback 使用传统回调
if (callBack) { if (callBack) {
@ -493,6 +534,26 @@ class API<I extends IAnyData, O extends IAnyData> extends Emitter<IAPIEvent<I, O
} }
} }
public wait(): Promise<IAPIResultEvent<O, U>["done"]>;
public wait(callBack: IResCallBack<O, U>): this;
public wait(callBack?: IResCallBack<O, U>): Promise<IAPIResultEvent<O, U>["done"]> | this {
// 存在 callback 使用传统回调
if (callBack) {
callBack.ok && this.on("ok", callBack.ok);
callBack.no && this.on("no", callBack.no);
callBack.done && this.on("done", callBack.done);
return this;
}
// 不存在 callback 使用 Promise 对象
else {
return new Promise((r) => {
this.on("done", r);
});
}
}
/** /**
* *
* @param message * @param message
@ -567,6 +628,12 @@ interface ICallBack<O extends IAnyData> {
complete?: (data: IAPIEvent<{}, O>["complete"]) => any; complete?: (data: IAPIEvent<{}, O>["complete"]) => any;
} }
interface IResCallBack<O extends IAnyData, U extends IAnyData> {
ok?: (data: IAPIResultEvent<O, U>["ok"]) => any;
no?: (data: IAPIResultEvent<O, U>["no"]) => any;
done?: (data: IAPIResultEvent<O, U>["done"]) => any;
}
/** /**
* Request * Request
* *

View File

@ -62,7 +62,7 @@ implements Partial<ILifetime> {
info: { info: {
data: "abc" data: "abc"
} }
}).request().wait({ }).request().waitRequest({
success: (d) => console.log(d) success: (d) => console.log(d)
}) })
} }