@ execution- flows/ flow- compose (英语)
安装
npm install @execution-flows/flow-compose
说明
在其核心, flow-compose 是一个轻而易举的功能 依赖性注射 库,它帮助您管理复杂的执行流程,将您的功能与它们的依赖关系脱钩.
它鼓励一种强大的模式,使您代码库中的每个功能都可以被写成一个 无效函数- 一个不需要参数的函数。 这意味着你可以从任何地方引用任何函数,而不用担心如何提供它需要运行的数据或其他函数.
功能不是传递依赖性作为论据(通常会导致"道具钻入"的实践),而是简单地声明它们所需要的. 这是通过下列方式实现的: 流量上下文。 。 。 .
高层 flow 函数作为 DI 容器。 您为它提供了一个上下文对象,该对象具有流量所需的全部函数和数据. 当一个函数被执行时,流会自动地注入它所声明的依赖性,使其可供使用.
这种模式的一个关键特征是: 懒惰装入 默认。 由于每个依赖都包含在一个函数中,除非明确援引,否则任何依赖都不予评估。 如果一个依赖从未被调用,那么它的代码就从未被执行,使得整个系统的效率非常高.
结果是: 可堆肥、可检验和可维护 代码.
使用量
整个图书馆只暴露出四个功能: flow, (中文(简体) ), flowFunction, (中文(简体) ), flowArgument,以及 flowProperty。然后,这四个功能可以用来构成任何复杂的执行流程,最明确地说,哪个代码需要什么以及如何得到它.
基本流动
这个 flow 财务报告和审定财务报表 flowFunction 构造是这个图书馆的构件.
import { flow, flowFunction } from "@execution-flows/flow-compose";
import type { FlowFunction } from "@execution-flows/flow-compose";
// A `flowFunction` is a unit of logic that can be used within a `flow`.
const greetHelloWorld = flowFunction(() => {
console.log("Hello, World!");
});
// The `flow` function composes everything into a single, callable function.
const helloWorld = flow(
// The first argument is the `context`, a map of named dependencies.
{ greet: greetHelloWorld },
// The second argument is the `body` of the flow, which is also a `flowFunction`.
flowFunction(({ greet }: { greet: FlowFunction<void> }) => {
greet();
}),
);
helloWorld(); // Outputs: "Hello, World!"
随参数流动
你用得着 flowArgument 在引用时向您的流量传递参数.
import { flow, flowFunction, flowArgument } from "@execution-flows/flow-compose";
import type { FlowFunction } from "@execution-flows/flow-compose";
// This `flowFunction` depends on `name`, which will be provided by a `flowArgument`.
const greetWithName = flowFunction(({ name }: { name: FlowFunction<string> }) => {
console.log(`Hello, ${name()}!`);
});
const hello = flow(
{
// `flowArgument` marks `name` as a required runtime argument for the final flow.
name: flowArgument<string>(),
// The context maps the implementation `greetWithName` to the name `greet`.
// Any `flowFunction` inside this flow can now request `greet` to get its dependency.
greet: greetWithName,
},
flowFunction(({ greet }: { greet: FlowFunction<void> }) => {
greet();
}),
);
// The resulting `hello` function now expects an object with a `name` property.
hello({ name: "Ada" }); // Outputs: "Hello, Ada!"
以参数构成函数
A 类 flowFunction 也可以提供接受参数的依赖. 这使得流中的其他功能可以注入并用不同的参数调用,从而能够更能动态地再用逻辑.
import { flow, flowFunction } from "@execution-flows/flow-compose";
// This `flowFunction` provides a dependency named `greetingBuilder`.
const greetingBuilder = flowFunction((): ((index: number) => string) => {
// The dependency it provides is a function that takes an `index`.
return (index: number) => {
console.log(`Building greeting for index: ${index}`);
return `Hello, World! - ${String(index)}`;
};
});
// This `flowFunction` depends on `greeting`, which it expects to be a function.
const greetUsingGreeting = flowFunction(
({ greeting }: { greeting: (index: number) => string }): void => {
// It can then call the injected function with different arguments.
const greetingOne = greeting(11);
const greetingTwo = greeting(13);
console.log(greetingOne);
console.log(greetingTwo);
},
);
const helloWorld = flow(
{ greeting: greetingBuilder, greet: greetUsingGreeting },
flowFunction(({ greet }: { greet: () => void }): void => {
greet();
}),
);
helloWorld();
// Outputs:
// Building greeting for index: 11
// Building greeting for index: 13
// Hello, World! - 11
// Hello, World! - 13
与缓存 flowProperty
使用 flowProperty 以创建一个结果被缓存的依赖 在一次援引中 流动。 这对于可以多次访问的昂贵操作是有用的.
import { flow, flowFunction, flowProperty } from "@execution-flows/flow-compose";
import type { FlowFunction } from "@execution-flows/flow-compose";
// `flowProperty` is a shorthand for a `flowFunction` with caching enabled.
// The inner function will only be executed once per flow run.
const greetingHelloWorld = flowProperty((): string => {
console.log("Generating greeting...");
return "Hello, World!";
});
const greetUsingGreeting = flowFunction(
({ greeting }: { greeting: FlowFunction<string> }): void => {
// Accessing `greeting` multiple times...
const greetingOnce = greeting();
const greetingTwice = greeting();
// ...does not re-run the original function.
console.log(greetingOnce);
console.log(greetingTwice);
},
);
const helloWorld = flow(
{ greeting: greetingHelloWorld, greet: greetUsingGreeting },
flowFunction(({ greet }: { greet: FlowFunction<void> }): void => {
greet();
}),
);
helloWorld();
// Outputs:
// Generating greeting...
// Hello, World!
// Hello, World!
缓存由混合的流 Flow
您可以使用 Flow 函数装饰器.
import { flow, flowFunction, Flow } from "@execution-flows/flow-compose";
import type { FlowFunction } from "@execution-flows/flow-compose";
const greetingHelloWorld = flowFunction((): string => {
console.log("Generating greeting...");
return "Hello, World!";
});
// This is a standard composed flow.
const helloWorldGreeting = flow(
{ greeting: greetingHelloWorld },
flowFunction(({ greeting }: { greeting: FlowFunction<string> }): string => {
return greeting();
}),
);
const greetUsingGreeting = flowFunction(
({ greeting }: { greeting: FlowFunction<string> }): void => {
greeting();
},
);
// The `Flow` function wraps another flow and applies options.
// Here, we cache the result of `helloWorldGreeting`.
const helloWorld = flow(
{ greeting: Flow(helloWorldGreeting, { cached: true }), greet: greetUsingGreeting },
flowFunction(({ greet }: { greet: FlowFunction<void> }): void => {
// `greet` will be called twice...
greet();
greet();
// ...but the underlying `helloWorldGreeting` flow will only run once.
}),
);
helloWorld();
// Outputs:
// Generating greeting...
捐款
贡献使得开放源码社区成为学习、激励和创造的惊人场所。 我们欢迎各种捐助!
如果您对新功能有建议, 找到错误, 或者看到改进文档的机会, 请随意 打开一个问题。 。 。 .
也非常感谢拉动请求.
社区和支助
有问题还是想分享一个想法? 开始交谈的最佳地点是 打开一个问题 (原始内容存档于2018-09-29). on GitHub. 我们很想听到你的消息!
许可证
@execution-flows/flow-compose 是一个开源软件,根据 MPL - 2.0 导弹。 。 。 .