nfkit 1.0.1
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/.eslintignore +4 -0
- package/.eslintrc.js +25 -0
- package/.prettierrc +4 -0
- package/LICENSE +22 -0
- package/README.md +3 -0
- package/dist/index.cjs +121 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +94 -0
- package/dist/index.mjs.map +7 -0
- package/dist/src/dual-object.d.ts +12 -0
- package/dist/src/workflow.d.ts +8 -0
- package/index.ts +1 -0
- package/package.json +61 -0
- package/tsconfig.json +20 -0
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
parser: '@typescript-eslint/parser',
|
|
3
|
+
parserOptions: {
|
|
4
|
+
project: 'tsconfig.json',
|
|
5
|
+
tsconfigRootDir : __dirname,
|
|
6
|
+
sourceType: 'module',
|
|
7
|
+
},
|
|
8
|
+
plugins: ['@typescript-eslint/eslint-plugin'],
|
|
9
|
+
extends: [
|
|
10
|
+
'plugin:@typescript-eslint/recommended',
|
|
11
|
+
'plugin:prettier/recommended',
|
|
12
|
+
],
|
|
13
|
+
root: true,
|
|
14
|
+
env: {
|
|
15
|
+
node: true,
|
|
16
|
+
jest: true,
|
|
17
|
+
},
|
|
18
|
+
ignorePatterns: ['.eslintrc.js'],
|
|
19
|
+
rules: {
|
|
20
|
+
'@typescript-eslint/interface-name-prefix': 'off',
|
|
21
|
+
'@typescript-eslint/explicit-function-return-type': 'off',
|
|
22
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
23
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
24
|
+
},
|
|
25
|
+
};
|
package/.prettierrc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Nanahira
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// index.ts
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
WF_NODE: () => WF_NODE,
|
|
23
|
+
workflow: () => workflow
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/workflow.ts
|
|
28
|
+
var Node = class _Node {
|
|
29
|
+
constructor(source, parent, op) {
|
|
30
|
+
this.source = source;
|
|
31
|
+
this.parent = parent;
|
|
32
|
+
this.op = op;
|
|
33
|
+
}
|
|
34
|
+
run() {
|
|
35
|
+
if (!this.memo) {
|
|
36
|
+
this.memo = (async () => {
|
|
37
|
+
if (!this.parent) {
|
|
38
|
+
return await this.source;
|
|
39
|
+
}
|
|
40
|
+
const base = await this.parent.run();
|
|
41
|
+
return this.op ? this.op(base) : base;
|
|
42
|
+
})();
|
|
43
|
+
}
|
|
44
|
+
return this.memo;
|
|
45
|
+
}
|
|
46
|
+
// 工厂:衍生子节点(分叉 = 复用同一个 parent)
|
|
47
|
+
child(op) {
|
|
48
|
+
return new _Node(this.source, this, op);
|
|
49
|
+
}
|
|
50
|
+
// 拿到“相同节点”的句柄(显式 fork)
|
|
51
|
+
fork() {
|
|
52
|
+
return new _Node(this.source, this.parent, this.op);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var WF_NODE = Symbol("@@workflow/node");
|
|
56
|
+
function isWorkflowChain(x) {
|
|
57
|
+
return !!x && typeof x === "function" && x[WF_NODE] instanceof Node;
|
|
58
|
+
}
|
|
59
|
+
function workflow(source) {
|
|
60
|
+
if (isWorkflowChain(source)) {
|
|
61
|
+
return source;
|
|
62
|
+
}
|
|
63
|
+
const root = new Node(source, null, null);
|
|
64
|
+
return makeProxy(root);
|
|
65
|
+
}
|
|
66
|
+
function makeProxy(node) {
|
|
67
|
+
const runOnce = () => node.run();
|
|
68
|
+
const rootHandler = {
|
|
69
|
+
get(_t, prop) {
|
|
70
|
+
if (prop === WF_NODE) return node;
|
|
71
|
+
if (prop === "then")
|
|
72
|
+
return (res, rej) => runOnce().then(res, rej);
|
|
73
|
+
if (prop === "catch") return (rej) => runOnce().catch(rej);
|
|
74
|
+
if (prop === "finally") return (fin) => runOnce().finally(fin);
|
|
75
|
+
return linkProxy(prop);
|
|
76
|
+
},
|
|
77
|
+
// 支持对“当前值”直接二次调用: foo()()
|
|
78
|
+
apply(_t, _this, argArray) {
|
|
79
|
+
const child = node.child((cur) => {
|
|
80
|
+
if (typeof cur !== "function")
|
|
81
|
+
throw new TypeError("Current value is not callable");
|
|
82
|
+
return cur(...argArray);
|
|
83
|
+
});
|
|
84
|
+
return makeProxy(child);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const linkProxy = (prop) => new Proxy(function() {
|
|
88
|
+
}, {
|
|
89
|
+
// 把 “.foo(…args)” 记录为 Call
|
|
90
|
+
apply(_t, _this, argArray) {
|
|
91
|
+
const child = node.child((cur) => {
|
|
92
|
+
const fn = cur[prop];
|
|
93
|
+
if (typeof fn !== "function") {
|
|
94
|
+
throw new TypeError(`${String(prop)} is not a function`);
|
|
95
|
+
}
|
|
96
|
+
return fn.apply(cur, argArray);
|
|
97
|
+
});
|
|
98
|
+
return makeProxy(child);
|
|
99
|
+
},
|
|
100
|
+
// 把 “.bar” 记录为 Get;继续深入时在这个 Get 的结果上再处理
|
|
101
|
+
get(_t, next) {
|
|
102
|
+
if (next === WF_NODE) return node;
|
|
103
|
+
if (next === "then")
|
|
104
|
+
return (r, j) => node.child((cur) => cur[prop]).run().then(r, j);
|
|
105
|
+
if (next === "catch")
|
|
106
|
+
return (rej) => node.child((cur) => cur[prop]).run().catch(rej);
|
|
107
|
+
if (next === "finally")
|
|
108
|
+
return (fin) => node.child((cur) => cur[prop]).run().finally(fin);
|
|
109
|
+
const child = node.child((cur) => cur[prop]);
|
|
110
|
+
return makeProxy(child)[next];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return new Proxy(function() {
|
|
114
|
+
}, rootHandler);
|
|
115
|
+
}
|
|
116
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
117
|
+
0 && (module.exports = {
|
|
118
|
+
WF_NODE,
|
|
119
|
+
workflow
|
|
120
|
+
});
|
|
121
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../index.ts", "../src/workflow.ts"],
|
|
4
|
+
"sourcesContent": ["export * from './src/workflow';\n", "// ========== \u7C7B\u578B\u5DE5\u5177 ==========\ntype AwaitedLike<T> = T extends Promise<infer R> ? R : T;\ntype AnyFn = (...args: any[]) => any;\n\ntype Chain<T> = Promise<AwaitedLike<T>> &\n (T extends AnyFn\n ? (...a: Parameters<T>) => Chain<AwaitedLike<ReturnType<T>>>\n : {}) & {\n [K in keyof T]: T[K] extends (...a: infer A) => infer R\n ? (...a: A) => Chain<AwaitedLike<R>>\n : Chain<AwaitedLike<T[K]>>;\n };\n\n// ========== \u6838\u5FC3\uFF1A\u4E0D\u53EF\u53D8\u8282\u70B9 ==========\nclass Node {\n private memo?: Promise<any>;\n constructor(\n private readonly source: any, // \u53EA\u6709\u6839\u8282\u70B9\u6709\u610F\u4E49\uFF1B\u5B50\u8282\u70B9\u53EF\u590D\u7528\u6839\u7684 source\n private readonly parent: Node | null, // \u524D\u7F00\n private readonly op: ((x: any) => any) | null, // \u4ECE parent \u7ED3\u679C\u5230\u672C\u8282\u70B9\u7ED3\u679C\u7684\u53D8\u6362\n ) {}\n\n run(): Promise<any> {\n if (!this.memo) {\n this.memo = (async () => {\n if (!this.parent) {\n // \u6839\uFF1A\u628A source \u89E3\u4E00\u6B21\u5373\u53EF\n return await this.source;\n }\n // \u5148\u62FF\u5230\u7236\u8282\u70B9\u7ED3\u679C\uFF08\u4F1A\u8BB0\u5FC6\u5316\uFF09\n const base = await this.parent.run();\n // \u5E94\u7528\u672C\u8282\u70B9\u7684\u53D8\u6362\uFF08\u5141\u8BB8\u8FD4\u56DE\u540C\u6B65\u6216\u5F02\u6B65\uFF09\n return this.op ? this.op(base) : base;\n })();\n }\n return this.memo;\n }\n\n // \u5DE5\u5382\uFF1A\u884D\u751F\u5B50\u8282\u70B9\uFF08\u5206\u53C9 = \u590D\u7528\u540C\u4E00\u4E2A parent\uFF09\n child(op: (x: any) => any): Node {\n return new Node(this.source, this, op);\n }\n\n // \u62FF\u5230\u201C\u76F8\u540C\u8282\u70B9\u201D\u7684\u53E5\u67C4\uFF08\u663E\u5F0F fork\uFF09\n fork(): Node {\n return new Node(this.source, this.parent, this.op);\n }\n}\n\nexport const WF_NODE = Symbol('@@workflow/node');\n\nfunction isWorkflowChain(x: any): x is Chain<any> {\n return !!x && typeof x === 'function' && x[WF_NODE] instanceof Node;\n}\n\n// ========== \u5BF9\u5916 API\uFF1Aworkflow ==========\nexport function workflow<T>(source: T | Promise<T>): Chain<T> {\n if (isWorkflowChain(source)) {\n return source as unknown as Chain<T>;\n }\n const root = new Node(source, null, null);\n return makeProxy<T>(root) as any;\n}\n\n// ========== \u4EE3\u7406\u5C42\uFF1A\u628A Node \u66B4\u9732\u6210\u94FE\u5F0F API ==========\nfunction makeProxy<T>(node: Node): Chain<T> {\n const runOnce = () => node.run(); // \u8BB0\u5FC6\u5316\u5728 Node \u5185\u90E8\n\n const rootHandler: ProxyHandler<any> = {\n get(_t, prop) {\n if (prop === WF_NODE) return node;\n // \u7ED3\u675F\u4FE1\u53F7\uFF1A\u6240\u6709 then/catch/finally \u590D\u7528\u540C\u4E00\u4E2A Promise\n if (prop === 'then')\n return (res: any, rej?: any) => runOnce().then(res, rej);\n if (prop === 'catch') return (rej: any) => runOnce().catch(rej);\n if (prop === 'finally') return (fin: any) => runOnce().finally(fin);\n\n // \u8BBF\u95EE\u4E00\u4E2A\u5C5E\u6027\uFF1A\u751F\u6210\u4E00\u4E2A\u201CGet\u201D\u5B50\u8282\u70B9\uFF0C\u4F46\u4E0D\u6267\u884C\n return linkProxy(prop);\n },\n // \u652F\u6301\u5BF9\u201C\u5F53\u524D\u503C\u201D\u76F4\u63A5\u4E8C\u6B21\u8C03\u7528\uFF1A foo()()\n apply(_t, _this, argArray) {\n const child = node.child((cur: any) => {\n if (typeof cur !== 'function')\n throw new TypeError('Current value is not callable');\n return cur(...argArray);\n });\n return makeProxy<any>(child);\n },\n };\n\n const linkProxy = (prop: PropertyKey) =>\n new Proxy(function () {}, {\n // \u628A \u201C.foo(\u2026args)\u201D \u8BB0\u5F55\u4E3A Call\n apply(_t, _this, argArray) {\n const child = node.child((cur: any) => {\n const fn = (cur as any)[prop];\n if (typeof fn !== 'function') {\n throw new TypeError(`${String(prop)} is not a function`);\n }\n return fn.apply(cur, argArray);\n });\n return makeProxy<any>(child);\n },\n // \u628A \u201C.bar\u201D \u8BB0\u5F55\u4E3A Get\uFF1B\u7EE7\u7EED\u6DF1\u5165\u65F6\u5728\u8FD9\u4E2A Get \u7684\u7ED3\u679C\u4E0A\u518D\u5904\u7406\n get(_t, next) {\n if (next === WF_NODE) return node;\n if (next === 'then')\n return (r: any, j?: any) =>\n node\n .child((cur: any) => (cur as any)[prop])\n .run()\n .then(r, j);\n if (next === 'catch')\n return (rej: any) =>\n node\n .child((cur: any) => (cur as any)[prop])\n .run()\n .catch(rej);\n if (next === 'finally')\n return (fin: any) =>\n node\n .child((cur: any) => (cur as any)[prop])\n .run()\n .finally(fin);\n\n const child = node.child((cur: any) => (cur as any)[prop]);\n return (makeProxy as any)(child)[next];\n },\n });\n\n return new Proxy(function () {}, rootHandler) as any;\n}\n"],
|
|
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,EAAE,IAAI;AAAA,IACvC;AAAA,EACF,CAAC;AAEH,SAAO,IAAI,MAAM,WAAY;AAAA,EAAC,GAAG,WAAW;AAC9C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/workflow';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// src/workflow.ts
|
|
2
|
+
var Node = class _Node {
|
|
3
|
+
constructor(source, parent, op) {
|
|
4
|
+
this.source = source;
|
|
5
|
+
this.parent = parent;
|
|
6
|
+
this.op = op;
|
|
7
|
+
}
|
|
8
|
+
run() {
|
|
9
|
+
if (!this.memo) {
|
|
10
|
+
this.memo = (async () => {
|
|
11
|
+
if (!this.parent) {
|
|
12
|
+
return await this.source;
|
|
13
|
+
}
|
|
14
|
+
const base = await this.parent.run();
|
|
15
|
+
return this.op ? this.op(base) : base;
|
|
16
|
+
})();
|
|
17
|
+
}
|
|
18
|
+
return this.memo;
|
|
19
|
+
}
|
|
20
|
+
// 工厂:衍生子节点(分叉 = 复用同一个 parent)
|
|
21
|
+
child(op) {
|
|
22
|
+
return new _Node(this.source, this, op);
|
|
23
|
+
}
|
|
24
|
+
// 拿到“相同节点”的句柄(显式 fork)
|
|
25
|
+
fork() {
|
|
26
|
+
return new _Node(this.source, this.parent, this.op);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var WF_NODE = Symbol("@@workflow/node");
|
|
30
|
+
function isWorkflowChain(x) {
|
|
31
|
+
return !!x && typeof x === "function" && x[WF_NODE] instanceof Node;
|
|
32
|
+
}
|
|
33
|
+
function workflow(source) {
|
|
34
|
+
if (isWorkflowChain(source)) {
|
|
35
|
+
return source;
|
|
36
|
+
}
|
|
37
|
+
const root = new Node(source, null, null);
|
|
38
|
+
return makeProxy(root);
|
|
39
|
+
}
|
|
40
|
+
function makeProxy(node) {
|
|
41
|
+
const runOnce = () => node.run();
|
|
42
|
+
const rootHandler = {
|
|
43
|
+
get(_t, prop) {
|
|
44
|
+
if (prop === WF_NODE) return node;
|
|
45
|
+
if (prop === "then")
|
|
46
|
+
return (res, rej) => runOnce().then(res, rej);
|
|
47
|
+
if (prop === "catch") return (rej) => runOnce().catch(rej);
|
|
48
|
+
if (prop === "finally") return (fin) => runOnce().finally(fin);
|
|
49
|
+
return linkProxy(prop);
|
|
50
|
+
},
|
|
51
|
+
// 支持对“当前值”直接二次调用: foo()()
|
|
52
|
+
apply(_t, _this, argArray) {
|
|
53
|
+
const child = node.child((cur) => {
|
|
54
|
+
if (typeof cur !== "function")
|
|
55
|
+
throw new TypeError("Current value is not callable");
|
|
56
|
+
return cur(...argArray);
|
|
57
|
+
});
|
|
58
|
+
return makeProxy(child);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const linkProxy = (prop) => new Proxy(function() {
|
|
62
|
+
}, {
|
|
63
|
+
// 把 “.foo(…args)” 记录为 Call
|
|
64
|
+
apply(_t, _this, argArray) {
|
|
65
|
+
const child = node.child((cur) => {
|
|
66
|
+
const fn = cur[prop];
|
|
67
|
+
if (typeof fn !== "function") {
|
|
68
|
+
throw new TypeError(`${String(prop)} is not a function`);
|
|
69
|
+
}
|
|
70
|
+
return fn.apply(cur, argArray);
|
|
71
|
+
});
|
|
72
|
+
return makeProxy(child);
|
|
73
|
+
},
|
|
74
|
+
// 把 “.bar” 记录为 Get;继续深入时在这个 Get 的结果上再处理
|
|
75
|
+
get(_t, next) {
|
|
76
|
+
if (next === WF_NODE) return node;
|
|
77
|
+
if (next === "then")
|
|
78
|
+
return (r, j) => node.child((cur) => cur[prop]).run().then(r, j);
|
|
79
|
+
if (next === "catch")
|
|
80
|
+
return (rej) => node.child((cur) => cur[prop]).run().catch(rej);
|
|
81
|
+
if (next === "finally")
|
|
82
|
+
return (fin) => node.child((cur) => cur[prop]).run().finally(fin);
|
|
83
|
+
const child = node.child((cur) => cur[prop]);
|
|
84
|
+
return makeProxy(child)[next];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return new Proxy(function() {
|
|
88
|
+
}, rootHandler);
|
|
89
|
+
}
|
|
90
|
+
export {
|
|
91
|
+
WF_NODE,
|
|
92
|
+
workflow
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/workflow.ts"],
|
|
4
|
+
"sourcesContent": ["// ========== \u7C7B\u578B\u5DE5\u5177 ==========\ntype AwaitedLike<T> = T extends Promise<infer R> ? R : T;\ntype AnyFn = (...args: any[]) => any;\n\ntype Chain<T> = Promise<AwaitedLike<T>> &\n (T extends AnyFn\n ? (...a: Parameters<T>) => Chain<AwaitedLike<ReturnType<T>>>\n : {}) & {\n [K in keyof T]: T[K] extends (...a: infer A) => infer R\n ? (...a: A) => Chain<AwaitedLike<R>>\n : Chain<AwaitedLike<T[K]>>;\n };\n\n// ========== \u6838\u5FC3\uFF1A\u4E0D\u53EF\u53D8\u8282\u70B9 ==========\nclass Node {\n private memo?: Promise<any>;\n constructor(\n private readonly source: any, // \u53EA\u6709\u6839\u8282\u70B9\u6709\u610F\u4E49\uFF1B\u5B50\u8282\u70B9\u53EF\u590D\u7528\u6839\u7684 source\n private readonly parent: Node | null, // \u524D\u7F00\n private readonly op: ((x: any) => any) | null, // \u4ECE parent \u7ED3\u679C\u5230\u672C\u8282\u70B9\u7ED3\u679C\u7684\u53D8\u6362\n ) {}\n\n run(): Promise<any> {\n if (!this.memo) {\n this.memo = (async () => {\n if (!this.parent) {\n // \u6839\uFF1A\u628A source \u89E3\u4E00\u6B21\u5373\u53EF\n return await this.source;\n }\n // \u5148\u62FF\u5230\u7236\u8282\u70B9\u7ED3\u679C\uFF08\u4F1A\u8BB0\u5FC6\u5316\uFF09\n const base = await this.parent.run();\n // \u5E94\u7528\u672C\u8282\u70B9\u7684\u53D8\u6362\uFF08\u5141\u8BB8\u8FD4\u56DE\u540C\u6B65\u6216\u5F02\u6B65\uFF09\n return this.op ? this.op(base) : base;\n })();\n }\n return this.memo;\n }\n\n // \u5DE5\u5382\uFF1A\u884D\u751F\u5B50\u8282\u70B9\uFF08\u5206\u53C9 = \u590D\u7528\u540C\u4E00\u4E2A parent\uFF09\n child(op: (x: any) => any): Node {\n return new Node(this.source, this, op);\n }\n\n // \u62FF\u5230\u201C\u76F8\u540C\u8282\u70B9\u201D\u7684\u53E5\u67C4\uFF08\u663E\u5F0F fork\uFF09\n fork(): Node {\n return new Node(this.source, this.parent, this.op);\n }\n}\n\nexport const WF_NODE = Symbol('@@workflow/node');\n\nfunction isWorkflowChain(x: any): x is Chain<any> {\n return !!x && typeof x === 'function' && x[WF_NODE] instanceof Node;\n}\n\n// ========== \u5BF9\u5916 API\uFF1Aworkflow ==========\nexport function workflow<T>(source: T | Promise<T>): Chain<T> {\n if (isWorkflowChain(source)) {\n return source as unknown as Chain<T>;\n }\n const root = new Node(source, null, null);\n return makeProxy<T>(root) as any;\n}\n\n// ========== \u4EE3\u7406\u5C42\uFF1A\u628A Node \u66B4\u9732\u6210\u94FE\u5F0F API ==========\nfunction makeProxy<T>(node: Node): Chain<T> {\n const runOnce = () => node.run(); // \u8BB0\u5FC6\u5316\u5728 Node \u5185\u90E8\n\n const rootHandler: ProxyHandler<any> = {\n get(_t, prop) {\n if (prop === WF_NODE) return node;\n // \u7ED3\u675F\u4FE1\u53F7\uFF1A\u6240\u6709 then/catch/finally \u590D\u7528\u540C\u4E00\u4E2A Promise\n if (prop === 'then')\n return (res: any, rej?: any) => runOnce().then(res, rej);\n if (prop === 'catch') return (rej: any) => runOnce().catch(rej);\n if (prop === 'finally') return (fin: any) => runOnce().finally(fin);\n\n // \u8BBF\u95EE\u4E00\u4E2A\u5C5E\u6027\uFF1A\u751F\u6210\u4E00\u4E2A\u201CGet\u201D\u5B50\u8282\u70B9\uFF0C\u4F46\u4E0D\u6267\u884C\n return linkProxy(prop);\n },\n // \u652F\u6301\u5BF9\u201C\u5F53\u524D\u503C\u201D\u76F4\u63A5\u4E8C\u6B21\u8C03\u7528\uFF1A foo()()\n apply(_t, _this, argArray) {\n const child = node.child((cur: any) => {\n if (typeof cur !== 'function')\n throw new TypeError('Current value is not callable');\n return cur(...argArray);\n });\n return makeProxy<any>(child);\n },\n };\n\n const linkProxy = (prop: PropertyKey) =>\n new Proxy(function () {}, {\n // \u628A \u201C.foo(\u2026args)\u201D \u8BB0\u5F55\u4E3A Call\n apply(_t, _this, argArray) {\n const child = node.child((cur: any) => {\n const fn = (cur as any)[prop];\n if (typeof fn !== 'function') {\n throw new TypeError(`${String(prop)} is not a function`);\n }\n return fn.apply(cur, argArray);\n });\n return makeProxy<any>(child);\n },\n // \u628A \u201C.bar\u201D \u8BB0\u5F55\u4E3A Get\uFF1B\u7EE7\u7EED\u6DF1\u5165\u65F6\u5728\u8FD9\u4E2A Get \u7684\u7ED3\u679C\u4E0A\u518D\u5904\u7406\n get(_t, next) {\n if (next === WF_NODE) return node;\n if (next === 'then')\n return (r: any, j?: any) =>\n node\n .child((cur: any) => (cur as any)[prop])\n .run()\n .then(r, j);\n if (next === 'catch')\n return (rej: any) =>\n node\n .child((cur: any) => (cur as any)[prop])\n .run()\n .catch(rej);\n if (next === 'finally')\n return (fin: any) =>\n node\n .child((cur: any) => (cur as any)[prop])\n .run()\n .finally(fin);\n\n const child = node.child((cur: any) => (cur as any)[prop]);\n return (makeProxy as any)(child)[next];\n },\n });\n\n return new Proxy(function () {}, rootHandler) as any;\n}\n"],
|
|
5
|
+
"mappings": ";AAcA,IAAM,OAAN,MAAM,MAAK;AAAA,EAET,YACmB,QACA,QACA,IACjB;AAHiB;AACA;AACA;AAAA,EAChB;AAAA,EAEH,MAAoB;AAClB,QAAI,CAAC,KAAK,MAAM;AACd,WAAK,QAAQ,YAAY;AACvB,YAAI,CAAC,KAAK,QAAQ;AAEhB,iBAAO,MAAM,KAAK;AAAA,QACpB;AAEA,cAAM,O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e,KAAK;AAAA,IAC7B;AAAA,EACF;AAEA,QAAM,YAAY,CAAC,SACjB,IAAI,MAAM,WAAY;AAAA,EAAC,GAAG;AAAA;AAAA,IAExB,MAAM,IAAI,OAAO,UAAU;AACzB,YAAM,QAAQ,KAAK,MAAM,CAAC,QAAa;AACrC,cAAM,KAAM,IAAY,IAAI;AAC5B,YAAI,OAAO,OAAO,YAAY;AAC5B,gBAAM,IAAI,UAAU,GAAG,OAAO,IAAI,CAAC,oBAAoB;AAAA,QACzD;AACA,eAAO,GAAG,MAAM,KAAK,QAAQ;AAAA,MAC/B,CAAC;AACD,aAAO,UAAe,KAAK;AAAA,IAC7B;AAAA;AAAA,IAEA,IAAI,IAAI,MAAM;AACZ,UAAI,SAAS,QAAS,QAAO;AAC7B,UAAI,SAAS;AACX,eAAO,CAAC,GAAQ,MACd,KACG,MAAM,CAAC,QAAc,IAAY,IAAI,CAAC,EACtC,IAAI,EACJ,KAAK,GAAG,CAAC;AAChB,UAAI,SAAS;AACX,eAAO,CAAC,QACN,KACG,MAAM,CAAC,QAAc,IAAY,IAAI,CAAC,EACtC,IAAI,EACJ,MAAM,GAAG;AAChB,UAAI,SAAS;AACX,eAAO,CAAC,QACN,KACG,MAAM,CAAC,QAAc,IAAY,IAAI,CAAC,EACtC,IAAI,EACJ,QAAQ,GAAG;AAElB,YAAM,QAAQ,KAAK,MAAM,CAAC,QAAc,IAAY,IAAI,CAAC;AACzD,aAAQ,UAAkB,KAAK,EAAE,IAAI;AAAA,IACvC;AAAA,EACF,CAAC;AAEH,SAAO,IAAI,MAAM,WAAY;AAAA,EAAC,GAAG,WAAW;AAC9C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type Dual<T> = T & PromiseLike<T>;
|
|
2
|
+
/** 仅允许填入 “返回 Promise 的方法名” */
|
|
3
|
+
export type AsyncMethodKeys<T> = {
|
|
4
|
+
[K in keyof T]-?: T[K] extends (...args: any[]) => Promise<any> ? K : never;
|
|
5
|
+
}[keyof T];
|
|
6
|
+
export interface DualizeOptions<T> {
|
|
7
|
+
/** 这些方法在 undecided/pending 时会返回一个延迟执行函数,等待对象 Promise 完成后再调用 */
|
|
8
|
+
asyncMethods?: readonly AsyncMethodKeys<T>[];
|
|
9
|
+
}
|
|
10
|
+
export declare function dualizeAny<T>(sync: () => T, // 同步构造;若抛错则视为 rejected
|
|
11
|
+
asyncFn: () => Promise<T>, // 异步构造
|
|
12
|
+
options?: DualizeOptions<T>): Dual<T>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type AwaitedLike<T> = T extends Promise<infer R> ? R : T;
|
|
2
|
+
type AnyFn = (...args: any[]) => any;
|
|
3
|
+
type Chain<T> = Promise<AwaitedLike<T>> & (T extends AnyFn ? (...a: Parameters<T>) => Chain<AwaitedLike<ReturnType<T>>> : {}) & {
|
|
4
|
+
[K in keyof T]: T[K] extends (...a: infer A) => infer R ? (...a: A) => Chain<AwaitedLike<R>> : Chain<AwaitedLike<T[K]>>;
|
|
5
|
+
};
|
|
6
|
+
export declare const WF_NODE: unique symbol;
|
|
7
|
+
export declare function workflow<T>(source: T | Promise<T>): Chain<T>;
|
|
8
|
+
export {};
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/workflow';
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nfkit",
|
|
3
|
+
"description": "Common kits",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"lint": "eslint --fix .",
|
|
10
|
+
"compile:cjs": "esbuild index.ts --outfile=dist/index.cjs --bundle --sourcemap --platform=node --target=es2019 --external:js-base64",
|
|
11
|
+
"compile:esm": "esbuild index.ts --outfile=dist/index.mjs --bundle --sourcemap --platform=neutral --target=esnext --external:js-base64",
|
|
12
|
+
"compile:types": "tsc --emitDeclarationOnly --declaration",
|
|
13
|
+
"build": "rimraf dist && npm run compile:cjs && npm run compile:esm && npm run compile:types",
|
|
14
|
+
"test": "jest --passWithNoTests",
|
|
15
|
+
"start": "node dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://code.moenext.com/nanahira/nfkit.git"
|
|
20
|
+
},
|
|
21
|
+
"author": "Nanahira <nanahira@momobako.com>",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"keywords": [],
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://code.moenext.com/nanahira/nfkit/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://code.moenext.com/nanahira/nfkit",
|
|
28
|
+
"jest": {
|
|
29
|
+
"moduleFileExtensions": [
|
|
30
|
+
"js",
|
|
31
|
+
"json",
|
|
32
|
+
"ts"
|
|
33
|
+
],
|
|
34
|
+
"rootDir": "tests",
|
|
35
|
+
"testRegex": ".*\\.spec\\.ts$",
|
|
36
|
+
"transform": {
|
|
37
|
+
"^.+\\.(t|j)s$": "ts-jest"
|
|
38
|
+
},
|
|
39
|
+
"collectCoverageFrom": [
|
|
40
|
+
"**/*.(t|j)s"
|
|
41
|
+
],
|
|
42
|
+
"coverageDirectory": "../coverage",
|
|
43
|
+
"testEnvironment": "node"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/jest": "^30.0.0",
|
|
47
|
+
"@types/node": "^24.9.1",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
49
|
+
"@typescript-eslint/parser": "^6.21.0",
|
|
50
|
+
"esbuild": "^0.25.11",
|
|
51
|
+
"esbuild-register": "^3.6.0",
|
|
52
|
+
"eslint": "8.22.0",
|
|
53
|
+
"eslint-config-prettier": "^9.1.2",
|
|
54
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
55
|
+
"jest": "^30.2.0",
|
|
56
|
+
"prettier": "^3.6.2",
|
|
57
|
+
"rimraf": "^6.0.1",
|
|
58
|
+
"ts-jest": "^29.4.5",
|
|
59
|
+
"typescript": "^5.9.3"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "dist",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"target": "es2021",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"emitDecoratorMetadata": true,
|
|
8
|
+
"experimentalDecorators": true,
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"sourceMap": true
|
|
11
|
+
},
|
|
12
|
+
"compileOnSave": true,
|
|
13
|
+
"allowJs": true,
|
|
14
|
+
"include": [
|
|
15
|
+
"*.ts",
|
|
16
|
+
"src/**/*.ts",
|
|
17
|
+
"test/**/*.ts",
|
|
18
|
+
"tests/**/*.ts"
|
|
19
|
+
]
|
|
20
|
+
}
|