npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

waterfall-unitwork

v1.0.7

Published

具有waterfall特性的工作函数

Downloads

10

Readme

介绍

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

使用场景

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

代码使用

// 串联请求
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判断是否业务处理成功
      })
// 根据进入微信小程序后解析的路径判断不同的业务
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) => {
        // 最终业务
      })