@ruan-cat/utils 1.1.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -8
- package/src/Prettify.ts +0 -1
- package/src/define-promise-tasks.ts +131 -0
- package/src/index.ts +4 -0
- package/src/simple-promise-tools.ts +55 -0
- package/src/types/pnpm-workspace.yaml.shim.ts +22 -0
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruan-cat/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "阮喵喵工具集合。一个纯typescript库。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
7
7
|
"types": "./src/index.ts",
|
|
8
|
-
"homepage": "https://github.com/ruan-cat/vercel-monorepo-test/tree/main/utils",
|
|
8
|
+
"homepage": "https://github.com/ruan-cat/vercel-monorepo-test/tree/main/packages/utils",
|
|
9
9
|
"bugs": {
|
|
10
10
|
"url": "https://github.com/ruan-cat/vercel-monorepo-test/issues"
|
|
11
11
|
},
|
|
@@ -38,14 +38,15 @@
|
|
|
38
38
|
"tsconfig.json"
|
|
39
39
|
],
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"
|
|
42
|
-
"typedoc
|
|
43
|
-
"
|
|
41
|
+
"@antfu/utils": "^0.7.10",
|
|
42
|
+
"typedoc": "^0.27.0",
|
|
43
|
+
"typedoc-plugin-markdown": "^4.3.0",
|
|
44
|
+
"typescript": "5.7.2",
|
|
44
45
|
"unplugin-vue-router": "^0.10.8",
|
|
45
|
-
"@ruan-cat/vuepress-preset-config": "^0.1.
|
|
46
|
+
"@ruan-cat/vuepress-preset-config": "^0.1.13"
|
|
46
47
|
},
|
|
47
48
|
"peerDependencies": {
|
|
48
|
-
"typescript": "5.
|
|
49
|
+
"typescript": "5.7.2",
|
|
49
50
|
"unplugin-vue-router": "^0.10.8"
|
|
50
51
|
},
|
|
51
52
|
"peerDependenciesMeta": {
|
|
@@ -59,7 +60,7 @@
|
|
|
59
60
|
"scripts": {
|
|
60
61
|
"copy-readme": "cpx readme.md docs",
|
|
61
62
|
"copy-changelog": "cpx CHANGELOG.md docs",
|
|
62
|
-
"typedoc": "typedoc",
|
|
63
|
+
"typedoc": "typedoc --options typedoc.config.mjs",
|
|
63
64
|
"prettier-docs-(invalid)": "prettier docs/**/*.md --write",
|
|
64
65
|
"docs:dev-main": "vuepress-vite dev docs",
|
|
65
66
|
"docs:dev": "turbo docs:dev",
|
|
@@ -71,6 +72,7 @@
|
|
|
71
72
|
"generate:type-2": "tsc -p tsconfig.json --composite false --declaration true --emitDeclarationOnly true",
|
|
72
73
|
"generate:type-3": "vue-tsc -p tsconfig.types.json",
|
|
73
74
|
"get-type": "pnpm clean:type && pnpm generate:type-3",
|
|
75
|
+
"build:-try-1": "pnpm clean:type && vue-tsc -p tsconfig.build.json",
|
|
74
76
|
"rm:node_modules": "rimraf node_modules"
|
|
75
77
|
}
|
|
76
78
|
}
|
package/src/Prettify.ts
CHANGED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { type SimpleAsyncTask } from "./simple-promise-tools.ts";
|
|
2
|
+
|
|
3
|
+
export const taskTypes = <const>["single", "parallel", "queue"];
|
|
4
|
+
|
|
5
|
+
export type TaskType = (typeof taskTypes)[number];
|
|
6
|
+
|
|
7
|
+
export interface BaseTask {
|
|
8
|
+
/** 任务类型 */
|
|
9
|
+
type: TaskType;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type Task = SimpleAsyncTask | TasksConfig;
|
|
13
|
+
|
|
14
|
+
export interface SingleTasks extends BaseTask {
|
|
15
|
+
type: "single";
|
|
16
|
+
tasks: Task;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ParallelTasks extends BaseTask {
|
|
20
|
+
type: "parallel";
|
|
21
|
+
tasks: Task[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface QueueTasks extends BaseTask {
|
|
25
|
+
type: "queue";
|
|
26
|
+
tasks: Task[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type TasksConfig = SingleTasks | ParallelTasks | QueueTasks;
|
|
30
|
+
|
|
31
|
+
export type PromiseTasksConfig = TasksConfig;
|
|
32
|
+
|
|
33
|
+
function isSingleTasks(config: TasksConfig): config is SingleTasks {
|
|
34
|
+
return config.type === "single";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isParallelTasks(config: TasksConfig): config is ParallelTasks {
|
|
38
|
+
return config.type === "parallel";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isQueueTasks(config: TasksConfig): config is QueueTasks {
|
|
42
|
+
return config.type === "queue";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isSimpleAsyncTask(config: Task): config is SimpleAsyncTask {
|
|
46
|
+
return typeof config === "function";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isTasksConfig(config: Task): config is TasksConfig {
|
|
50
|
+
return typeof config === "object";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 定义异步任务对象
|
|
55
|
+
* @description
|
|
56
|
+
* 这个对象是一揽子异步任务的配置
|
|
57
|
+
*/
|
|
58
|
+
export function definePromiseTasks(config: TasksConfig) {
|
|
59
|
+
return config;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @private 一个工具函数 用于生成异步函数数组
|
|
64
|
+
* @deprecated 在处理串行任务时 疑似有故障
|
|
65
|
+
*/
|
|
66
|
+
function getPromises(tasks: Task[]): ((...args: any) => Promise<any>)[] {
|
|
67
|
+
return tasks.map((task) => {
|
|
68
|
+
return async function (...args: any) {
|
|
69
|
+
if (isSimpleAsyncTask(task)) {
|
|
70
|
+
return await task(...args);
|
|
71
|
+
} else {
|
|
72
|
+
return await executePromiseTasks(task);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 执行异步函数对象
|
|
80
|
+
*/
|
|
81
|
+
export async function executePromiseTasks(
|
|
82
|
+
config: TasksConfig,
|
|
83
|
+
/**
|
|
84
|
+
* 上一次递归执行时提供的参数
|
|
85
|
+
* @description
|
|
86
|
+
* 考虑到递归函数 这里提供了一个参数 用于传递上一次递归执行的结果
|
|
87
|
+
*/
|
|
88
|
+
lastParams: any = null,
|
|
89
|
+
): Promise<any> {
|
|
90
|
+
if (isSingleTasks(config)) {
|
|
91
|
+
if (isSimpleAsyncTask(config.tasks)) {
|
|
92
|
+
// 实际执行的 tasks 往往是无参函数 这里为了保险,故主动传递参数
|
|
93
|
+
return await config.tasks(lastParams);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return await executePromiseTasks(config.tasks, lastParams);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (isParallelTasks(config)) {
|
|
100
|
+
return await Promise.all(
|
|
101
|
+
config.tasks.map((task) => {
|
|
102
|
+
if (isSimpleAsyncTask(task)) {
|
|
103
|
+
// console.log(` 并行任务遇到单独的异步函数 `);
|
|
104
|
+
return task(lastParams);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// console.log(` 并行任务遇到嵌套结构 `);
|
|
108
|
+
return executePromiseTasks(task, lastParams);
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (isQueueTasks(config)) {
|
|
114
|
+
let res: Awaited<any>;
|
|
115
|
+
for await (const task of config.tasks) {
|
|
116
|
+
if (isSimpleAsyncTask(task)) {
|
|
117
|
+
// console.log(` 串行任务遇到单独的异步函数 `);
|
|
118
|
+
|
|
119
|
+
res = await task(lastParams);
|
|
120
|
+
lastParams = res;
|
|
121
|
+
// console.log(` 串行任务 单独 res `, res);
|
|
122
|
+
} else {
|
|
123
|
+
res = await executePromiseTasks(task, lastParams);
|
|
124
|
+
lastParams = res;
|
|
125
|
+
// console.log(` 串行任务 配置 res `, res);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return res;
|
|
130
|
+
}
|
|
131
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,3 +2,7 @@ export * from "./rmmv-class-expand-tools.ts";
|
|
|
2
2
|
export * from "./conditions.ts";
|
|
3
3
|
export * from "./Prettify.ts";
|
|
4
4
|
export * from "./unplugin-vue-router/index.ts";
|
|
5
|
+
export * from "./define-promise-tasks.ts";
|
|
6
|
+
export * from "./simple-promise-tools.ts";
|
|
7
|
+
|
|
8
|
+
export * from "./types/pnpm-workspace.yaml.shim.ts";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// import { uniqueId } from "lodash-es";
|
|
2
|
+
// const getCounter = () => uniqueId();
|
|
3
|
+
|
|
4
|
+
/** 创建简单的异步任务 */
|
|
5
|
+
export function generateSimpleAsyncTask<T extends (...args: any) => any>(func: T) {
|
|
6
|
+
// const taskId = getCounter();
|
|
7
|
+
|
|
8
|
+
return function (...args: any) {
|
|
9
|
+
// consola.info(` 这是第 ${taskId} 个异步任务 `);
|
|
10
|
+
// consola.start(" 这里是新创建的异步函数 检查参数: ", ...args);
|
|
11
|
+
|
|
12
|
+
return new Promise<ReturnType<T>>((resolve, reject) => {
|
|
13
|
+
// consola.start(" 内部promise 检查参数: ", ...args);
|
|
14
|
+
resolve(func(...args));
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type SimpleAsyncTask = ReturnType<typeof generateSimpleAsyncTask>;
|
|
20
|
+
|
|
21
|
+
export const initFlag = <const>"initFlag";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 以队列串行的形式 串行运行异步函数
|
|
25
|
+
* @see https://github.com/ascoders/weekly/blob/master/前沿技术/77.精读《用%20Reduce%20实现%20Promise%20串行执行》.md
|
|
26
|
+
* @version 1
|
|
27
|
+
*/
|
|
28
|
+
async function runPromiseByQueueV1<T>(promises: ((...args: any) => Promise<T>)[]) {
|
|
29
|
+
promises.reduce(
|
|
30
|
+
async function (previousPromise, nextPromise, currentIndex) {
|
|
31
|
+
const response = await previousPromise;
|
|
32
|
+
// consola.log(` reduce串行函数 currentIndex= ${currentIndex} res =`, response);
|
|
33
|
+
return await nextPromise(response);
|
|
34
|
+
},
|
|
35
|
+
Promise.resolve(initFlag) as Promise<any>,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 以队列串行的形式 串行运行异步函数
|
|
41
|
+
* @version 2
|
|
42
|
+
*/
|
|
43
|
+
export async function runPromiseByQueue<T>(promises: ((...args: any) => Promise<T>)[]) {
|
|
44
|
+
let response: typeof initFlag | Awaited<T> = initFlag;
|
|
45
|
+
for await (const promise of promises) {
|
|
46
|
+
response = await promise(response);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 以并行的形式 并发运行异步函数
|
|
52
|
+
*/
|
|
53
|
+
export async function runPromiseByConcurrency<T>(promises: ((...args: any) => Promise<T>)[]) {
|
|
54
|
+
await Promise.all(promises.map((promise) => promise()));
|
|
55
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pnpm-workspace.yaml 文件的类型声明
|
|
3
|
+
* @description
|
|
4
|
+
* 设计理由
|
|
5
|
+
*
|
|
6
|
+
* 主要是为了让该文件被解析后,能够有一个基础的类型声明
|
|
7
|
+
*
|
|
8
|
+
* 按理说这个东西应该有别人封装好的类型的,肯定因为我没找到。
|
|
9
|
+
*
|
|
10
|
+
* 未来应该找到这样的类型声明库,直接复用别人的就好了,不要自己写了。
|
|
11
|
+
*/
|
|
12
|
+
export interface PnpmWorkspace {
|
|
13
|
+
/**
|
|
14
|
+
* 包的匹配语法字符串
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ["packages/**", "demos/**", "utils"]
|
|
18
|
+
*/
|
|
19
|
+
packages?: string[];
|
|
20
|
+
|
|
21
|
+
catalog?: string[];
|
|
22
|
+
}
|