chore: utils and type definition

This commit is contained in:
yangsy
2025-08-29 11:41:01 +08:00
parent d948c96047
commit 73776c1fd1
3 changed files with 44 additions and 4 deletions

7
src/type.d.ts vendored Normal file
View File

@@ -0,0 +1,7 @@
export type Optional<T> = {
[P in keyof T]?: T[P] | null;
};
export type Nullable<T> = {
[P in keyof T]: T[P] | null;
};

View File

@@ -48,15 +48,20 @@ export class Request {
return this.instance; return this.instance;
} }
get<T>(url: string, option?: AxiosRequestConfig): Promise<Response<T>> { get<T>(url: string, option?: AxiosRequestConfig & { retRaw?: boolean }): Promise<Response<T>> {
const reqConfig = option ?? {}; const { retRaw, ...reqConfig } = option ?? {};
return new Promise((resolve) => { return new Promise((resolve) => {
this.instance this.instance
.get<Result<T>>(url, { .get(url, {
...reqConfig, ...reqConfig,
}) })
.then((res) => { .then((res) => {
resolve([null, res.data.data, res.data]); if (retRaw) {
resolve([null, res.data as T, null]);
} else {
const resData = res.data as Result<T>;
resolve([null, resData.data, resData]);
}
}) })
.catch((err) => { .catch((err) => {
resolve([err as AxiosError, null, null]); resolve([err as AxiosError, null, null]);

28
src/utils/sleep.ts Normal file
View File

@@ -0,0 +1,28 @@
// 宏任务,下一个事件循环周期执行
export const sleep = (ms: number = 0) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
// 微任务版本 - 最快让出控制权
// 微任务,当前事件循环周期的末尾执行
export const sleepMicrotask = () => {
return new Promise((resolve) => queueMicrotask(() => resolve));
};
// 空闲时执行版本 - 性能友好
export const sleepIdle = () => {
return new Promise((resolve) => {
if ('requestIdleCallback' in window) {
requestIdleCallback(resolve);
} else {
// fallback to setTimeout
setTimeout(resolve, 0);
}
});
};
// 帧同步版本 - 适合动画
// 在下一次重绘前执行
export const sleepFrame = () => {
return new Promise((resolve) => requestAnimationFrame(resolve));
};