# @execution-flows/flow-compose 中文文档

规范地址：https://npmcn.com/package/%40execution-flows/flow-compose/

版本：0\.3\.0
npm 元数据更新时间：2026-04-24T01:07:09.438Z
中文译文更新时间：2026-11-14T03:24:00.275Z
软件包许可：MPL-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40execution-flows/flow-compose
原文对照：https://npmcn.com/package/%40execution-flows/flow-compose/?lang=original
项目主页：https://github.com/execution-flows/flow-compose-ts#readme
源码仓库：https://github.com/execution-flows/flow-compose-ts

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：61.5 kB，39 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2026-03-22T15:35:00.575Z
- 最新版本发布：2026-04-24T01:07:09.270Z
- 近 30 天下载量：4160（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：6（近 12 个月 6 个）

## 中文简介

使用"流程编译"模式执行流程的TypeScript执行.

## 安装

```sh
npm install @execution-flows/flow-compose
```

也可以使用：`pnpm add @execution-flows/flow-compose`、`yarn add @execution-flows/flow-compose`、`bun add @execution-flows/flow-compose`

## README

# @ execution- flows/ flow- compose (英语)

## 安装

```bash
npm install @execution-flows/flow-compose
```

## 说明

在其核心, `flow-compose` 是一个轻而易举的功能 **依赖性注射** 库,它帮助您管理复杂的执行流程,将您的功能与它们的依赖关系脱钩.

它鼓励一种强大的模式,使您代码库中的每个功能都可以被写成一个 **无效函数**\- 一个不需要参数的函数。 这意味着你可以从任何地方引用任何函数,而不用担心如何提供它需要运行的数据或其他函数.

功能不是传递依赖性作为论据(通常会导致"道具钻入"的实践),而是简单地声明它们所需要的. 这是通过下列方式实现的: **流量上下文**。 。 。 .

高层 `flow` 函数作为 DI 容器。 您为它提供了一个上下文对象,该对象具有流量所需的全部函数和数据. 当一个函数被执行时,流会自动地注入它所声明的依赖性,使其可供使用.

这种模式的一个关键特征是: **懒惰装入** 默认。 由于每个依赖都包含在一个函数中,除非明确援引,否则任何依赖都不予评估。 如果一个依赖从未被调用,那么它的代码就从未被执行,使得整个系统的效率非常高.

结果是: **可堆肥、可检验和可维护** 代码.

![](https://npmcn.com/package/%40execution-flows/flow-compose/docs/images/composed-flow-dependency-1.png)

## 使用量

整个图书馆只暴露出四个功能: `flow`, (中文(简体) ), `flowFunction`, (中文(简体) ), `flowArgument`,以及 `flowProperty`。然后,这四个功能可以用来构成任何复杂的执行流程,最明确地说,哪个代码需要什么以及如何得到它.

### 基本流动

这个 `flow` 财务报告和审定财务报表 `flowFunction` 构造是这个图书馆的构件.

```typescript
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` 在引用时向您的流量传递参数.

```typescript
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` 也可以提供接受参数的依赖. 这使得流中的其他功能可以注入并用不同的参数调用,从而能够更能动态地再用逻辑.

```typescript
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` 以创建一个结果被缓存的依赖 _在一次援引中_ 流动。 这对于可以多次访问的昂贵操作是有用的.

```typescript
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` 函数装饰器.

```typescript
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 导弹**。 。 。 .

## 常见问题

### @execution-flows/flow-compose 是什么？

使用"流程编译"模式执行流程的TypeScript执行\.

### 如何安装 @execution-flows/flow-compose？

在项目目录运行 npm install @execution-flows/flow-compose，或使用 pnpm add / yarn add / bun add。

### @execution-flows/flow-compose 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @execution-flows/flow-compose 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @execution-flows/flow-compose 可以商用吗？

@execution-flows/flow-compose 使用 MPL-2\.0 许可。弱 Copyleft：可以商用，也可以和闭源代码一起使用；但如果分发时修改了 MPL 授权的文件，这些文件的修改需以 MPL 开源。以上为概括，不构成法律意见，请以许可原文为准。

### @execution-flows/flow-compose 的下载量有多少？

根据 npm 官方下载统计，@execution-flows/flow-compose 在 2026/10/14 – 2026/11/12 这 30 天内被下载 4,160 次，日均约 139 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @execution-flows/flow-compose 还在维护吗？

最新版本 0\.3\.0 发布于 2026/04/24（6 个月前）；至今共发布 6 个版本，其中过去 12 个月发布了 6 个；首次发布于 2026/03/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

