# waterfall-unitwork

> 具有waterfall特性的工作函数

Latest version **1.0.7** (published 2023-01-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install waterfall-unitwork
pnpm add waterfall-unitwork
yarn add waterfall-unitwork
bun add waterfall-unitwork
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2023-01-31 |
| First published | 2023-01-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | JanderFan |
| Maintainers | fanjinde |

## Links

- npm: https://www.npmjs.com/package/waterfall-unitwork
- Repository: https://gitee.com/fanjinde/waterfall-unitwork
- npm.io page: https://npm.io/package/waterfall-unitwork

## Dependencies (1)

- [@babel/runtime-corejs2](https://npm.io/package/@babel/runtime-corejs2.md) ^7.20.13

## Recent versions

- 1.0.7 (latest) — 2023-01-31
- 1.0.6 — 2023-01-31
- 1.0.5 — 2023-01-29
- 1.0.4 — 2023-01-27
- 1.0.3 — 2023-01-27
- 1.0.2 — 2023-01-27
- 1.0.1 — 2023-01-26
- 1.0.0 — 2023-01-26

## README

# 介绍

工作性质类似middleware，具有上下文的特性

## 使用场景

1. 串联请求
2. 按流程处理业务

## 代码使用

```typescript
// 串联请求
import WaterfallUnitWork from 'waterfall-unitwork';

// 明确上下文数据
type Data = {
  deptId?: string;
  users?: []
}

const tandemRequest = new WaterfallUnitWork<Data>({})
tandemRequest
      .add((ctx) => {
        // 请求部门 复制
        fetch('getDepeId').then((res) => {
          if (res.code === 200) {
            ctx.data.deptId = res.deptId
            // flag为true，代表进入下一个函数
            ctx.next(true)
          } else {
            // ctx为false，代表进入end函数
            // 错误信息也可以定义在ctx.data上
            ctx.next(false)
          }
        })
      })
      .add((ctx) => {
        console.log('ctx.data.deptId', ctx.data.deptId)
        // 根据ctx.data.deptId请求人员
      })
      .end((flag, ctx) => {
        // 根据flag判断是否业务处理成功
      })

```

```typescript
// 根据进入微信小程序后解析的路径判断不同的业务
import WaterfallUnitWork from 'waterfall-unitwork';
import type {UnitWorkFn} from 'waterfall-unitwork'
/**
     * 登录节点
     * 判断是否登录
     * 类似的方法可重复声明，而后重复使用
     */
    const loginNode: UnitWorkFn<Record<string, any>> = (ctx) => {
      // 判断是否登录
      if (hasLogin()) {
        ctx.next(true)
      } else {
        // 1. 登录
        // 进行下一个节点
        ctx.next(true)
      }
    }
    // task1需要登录
    const task1 = new WaterfallUnitWork<Record<string, any>>({})
    // task2 需要登录
    const task2 = new WaterfallUnitWork<Record<string, any>>({})
    task1
      .add(loginNode)
      .add((ctx) => {
        // 其他业务
      })
      .end((flag, ctx) => {
        // 处理最终
      })
    task2
      .add((ctx) => {
        // 业务1
      })
      .add((ctx) => {
        // 业务2
      })
      .end((falg, ctx) => {
        // 最终业务
      })

```

---
_Source: https://npm.io/package/waterfall-unitwork · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
