aocudeo
v4.1.2
Published
Process organizer of asynchronous or/and synchronous interdependent code units
Downloads
37
Maintainers
Readme
Aocudeo
通过注册位置信息并在其上绑定工作器回调,本包可用于以正确的顺序组织异步或同步代码流程。
An Organizer of Code Units that Depend on Each Other - Aocudeo, can organize your pipelining work with the correct order through the 'Position' registered by 'Workers'.
用例 Usage
加载插件 Loading Plugins
在索引模块中弄个加载器用来加载模块。
Get a public loader for your plugins in the index.
import Loader from 'aocudeo/async'; export const loader = new Loader({ reuseable: false });
根据插件名称和位置信息插入插件。
Insert the plugin with its name and dependencies' name.
loader .addPosition('foo', { after: ['bar', 'baz'], preOf: 'foobar', }) .addWorker('foo', async () => { await import('./plugins/foo.ts'); });
loader.execute();
简单流程工作 Simple Pipeline Work
借以 p-graph 的示例代码为例。
Do what p-graph's sample code do.
import Organizer, { Positions } from 'aocudeo/async';
const workers = new Map([
["putOnShirt", { run: () => Promise.resolve("put on your shirt") }],
["putOnShorts", { run: () => Promise.resolve("put on your shorts") }],
["putOnJacket", { run: () => Promise.resolve("put on your jacket") }],
["putOnShoes", { run: () => Promise.resolve("put on your shoes") }],
["tieShoes", { run: () => Promise.resolve("tie your shoes") }],
]);
const positions: Positions = [
// You need to put your shoes on before you tie them!
["putOnShoes", "tieShoes"],
["putOnShirt", "putOnJacket"],
["putOnShorts", "putOnJacket"],
["putOnShorts", "putOnShoes"],
];
await new Organizer({ workers, positions }).execute();
流水线处理 Pipeline Process
以下这个用例导出一个用于得到一个穿好衣服的人的函数 getPerson
。
The following usage exports getPerson
to obtain a person dressed properly.
import Organizer from "aocudeo/async";
export interface Person {
shorts?: 'shorts';
shoes?: 'shoes';
tied: boolean;
}
const organizer = new Organizer<Person>()
.addPositions({
putOnShoes: 'putOnShorts',
tieShoes: { postOf: 'putOnShoes' },
})
.addWorkers({
async putOnShorts({ data: person }) {
person.shorts = await Promise.resolve('shorts');
},
async putOnShoes({ data: person }) {
person.shoes = await Promise.resolve('shoes');
},
async tieShoes({ data: person }) {
person.tied = await Promise.resolve(true);
},
});
export async function getPerson() {
return await organizer.execute({ tied: false });
}
链接 Links
功能上与本包类似。
Functionally similar to this package.