Skip to content

Commit

Permalink
feat: 修复发包问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 committed Aug 11, 2024
1 parent 5cd2c36 commit 3fa7bb1
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"simple-git-hooks": "^2.8.1",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.1.6",
"vitest": "^0.33.0",
"zx": "^7.2.3"
Expand Down
1 change: 0 additions & 1 deletion packages/npm/package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"build": "tsup",
"test": "vitest",
"preinstall": "npx only-allow pnpm",
"postinstall": "simple-git-hooks",
"typecheck": "tsc -p scripts --noEmit && pnpm -r --parallel run typecheck",
"lint": "eslint --cache ./packages/*",
"format": "prettier --write --cache .",
Expand Down
1 change: 0 additions & 1 deletion packages/npm/publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"dev": "tsup --watch",
"build": "tsup",
"preinstall": "npx only-allow pnpm",
"postinstall": "simple-git-hooks",
"typecheck": "tsc -p scripts --noEmit && pnpm -r --parallel run typecheck",
"lint": "eslint --cache ./packages/*",
"format": "prettier --write --cache .",
Expand Down
1 change: 0 additions & 1 deletion packages/run/task/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"dev": "tsup --watch",
"build": "tsup",
"preinstall": "npx only-allow pnpm",
"postinstall": "simple-git-hooks",
"typecheck": "tsc -p scripts --noEmit && pnpm -r --parallel run typecheck",
"lint": "eslint --cache ./packages/*",
"format": "prettier --write --cache .",
Expand Down
23 changes: 14 additions & 9 deletions packages/run/task/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
type Method<S> = (state: S, n: number, carryFunction?: Function) => S | undefined;
type Method<S> = (
state: S,
n: number,
carryFunction?: Function,
) => S | undefined;

type Methods<S> = Record<string, Method<S>>;

Expand All @@ -15,18 +19,20 @@ const fi = <S, T extends Methods<S>>(
if (prop in methods) {
return (...args: any[]) => {
try {
// @ts-ignore
const result = methods[prop as keyof T](target, ...args);

if (result === undefined) {
console.warn(
`Method ${String(prop)} returned undefined, chain interrupted.`,
);
return undefined; // 返回 undefined 以中断链式调用
return undefined;
}
} catch (error) {
console.error(`Error occurred in method ${String(prop)}:`, error);
return undefined; // 发生错误时中断链式调用
return undefined;
}
return receiver; // 返回代理对象本身,支持链式调用
return receiver;
};
}
return Reflect.get(target, prop, receiver);
Expand All @@ -36,27 +42,26 @@ const fi = <S, T extends Methods<S>>(
return proxy as Chainable<S, T>;
};

// 示例:状态对象包含一个数值
interface ValueState {
value: number;
}

// 使用泛型创建链式调用对象实例
const chainable = fi<ValueState, Methods<ValueState>>(
{ value: 0 },
// @ts-ignore
{ add, subtract, multiply, divide, testInterrupt },
);

// 使用链式调用执行一系列计算 (包含错误处理)
// @ts-ignore
const result = chainable
.add(10)
.subtract(5)
.multiply(4)
.testInterrupt(100) // 这里会触发中断,后续操作不再执行
.testInterrupt(100)
?.add(10);

if (result) {
console.log(result.value); // 如果链式调用没有被中断,输出最终结果
console.log(result.value);
} else {
console.log('链式调用被中断');
}
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3fa7bb1

Please sign in to comment.