[VueJsDev] 目录列表

https://www.cnblogs.com/pengchenggang/p/17037320.html

asyncTool.js 异步执行工具

::: details 目录

:::

所有的业务逻辑,超过两个后,均要用此工具编写逻辑

Step. 1: getAc 使用方法

  1. 注册 asyncTool
// src/main.js
import asyncTool from "@/libs/common/asyncTool"
Vue.prototype.$getAc = () => {
return new asyncTool()
}
  1. 常规使用方法
const ac = this.$getAc()
ac.use(this.businessLogic1)
ac.use(this.businessLogic2)
ac.run() // methods 里面
businessLogic1 (ctx, next) {
// 业务逻辑1
next()
},
businessLogic2 (ctx, next) {
// 业务逻辑2
next()
},

Meth. 2: use 方法

作用:添加一个函数进入序列

目的:注入上下文ctx 和 next 进入函数体

const ac = this.$getAc()
ac.ctx.abc = 'hello'
ac.use(this.businessLogic1)
ac.run()
// methods 里面
businessLogic1 (ctx, next) {
// ctx 是上下文
// next 是指向下一个函数 使用方法 next()
console.info('ctx.abc', ctx.abc) // hello
}

Meth. 3: run 方法

作用:执行当前方法序列

目的:执行方法序列,可以添加最后一个lastCallBack

const ac = this.$getAc()
ac.use(this.businessLogic1)
ac.use(this.businessLogic2)
ac.use(this.businessLogic3)
// ac.run() // 调用方式1
ac.run(() => { // 调用方式2
// ac对象可以进行分发传递,最后可以进行最后一次尾调用
// 如果用use添加后再run,会产生重复use添加的问题
// doSomething
})

Meth. 4: nextJump 方法

作用:跳过下一个方法

目的:常见于根据条件是否执行下一个方法

businessLogic1 (ctx, next) {
// 业务逻辑1
if (a === 'ok') {
next()
} else {
ctx.nextJump() // 默认跳过下一个方法
}
},

Meth. 5: goto 方法

作用:跳到指定的方法

目的:常见于大跳转的业务逻辑,执行完本逻辑要执行最后几个公共逻辑,跳过中间的check

const ac = this.$getAc()
ac.use(this.businessLogic1)
ac.use(this.businessLogic2)
ac.use(this.businessLogic3)
ac.use(this.businessLogic4)
ac.use(this.businessLogic5)
ac.use(this.businessLogic6, { ref: 'businessLogic6'})
ac.use(this.businessLogic7)
ac.run() // methods 里面
businessLogic2 (ctx, next) {
// 业务逻辑2
if (a === 'ok') {
next()
} else {
ctx.goto('businessLogic6') // 跳到指定的业务逻辑
}
}

Meth. 6: ifTo 方法

作用:跳过当前方法

目的:常见于条件内部直接判断

// methods 里面
businessLogic1 (ctx, next) {
// 算是个语法糖 目的为占用一行 使得代码简洁
// 加叹号求反的目的是 如果条件不符合 就next,条件符合就执行后面的代码
if (ctx.ifTo(!ctx.modifyIsTrue, next)) return
// 其他业务代码...
}

Meth. 7: 批量动态调用接口

对于一个数组,批量调用接口

getUnLocalNames (ctx, next) {
// 新建和改名字没保存的,不能进行全部保存
let unLocalNames = []
this.tagListByR.forEach(item => {
if (item.type === 'new') {
unLocalNames.push(item.tagName)
}
}) const ac = this.$getAc()
ac.ctx.unLocalNames = unLocalNames
this.tagListByR.forEach(item => {
if (item.type === 'modify') {
ac.use((ctx1, next1) => {
apiRequest('getAppoint', { id: item.filePath }).then(data => {
if (item.tagName !== data.xingMing) {
ctx1.unLocalNames.push(item.tagName)
}
next1()
}, err => {
alert(err)
})
})
}
})
ac.run(() => {
ctx.unLocalNames = unLocalNames
next()
})
},

Code. 8: asyncTool.js 源码

// @/libs/common/asyncTool.js
// 创建时间 2020.03.11
// 更新于 2022.08.29 class asyncTool {
// eslint-disable-next-line
constructor() {
this.arr = []
this.ctx = {
goto: this.goto,
_root: this,
ifTo: this.ifTo,
nextJump: this.nextJump,
}
this.cIndex = 0
this.next = null
} nextJump(number = 1) {
this._root.cIndex = this._root.cIndex + number + 1
const one = this._root.arr[this._root.cIndex]
console.info(
`%cnextJump one 跳过后 ${number}个 的执行函数是: ${one.func.name} `,
"color:green",
)
this._root._innerRun(one.func, one.next)
} ifTo(boolValue, next) {
if (boolValue) {
next()
return true
} else {
return false
}
} goto(funcName) {
let runIndex = -1
console.info("this.arr", this)
this._root.arr.map((item, index) => {
if (item.ref === funcName) {
runIndex = index
}
})
if (runIndex !== -1) {
this._root.cIndex = runIndex
this._root._innerRun(
this._root.arr[runIndex].func,
this._root.arr[runIndex].next,
)
} else {
console.error("未找到goto方法名为" + funcName + "的函数")
}
} use(func, outParams) {
const initParams = {
ref: "",
}
const params = { ...initParams, ...outParams }
const into = {
...params,
func,
next: () => {},
}
this.arr.push(into)
if (this.arr.length > 1) {
const index = this.arr.length - 2
const nextFunc = () => {
// console.info('func.length', func.length)
this.cIndex = index + 1
this._innerRun(func, this.arr[index + 1].next)
}
this.arr[index].next = nextFunc
}
return this
} getFuncName(fun) {
// console.info('fun', fun.toString())
var ret = fun.toString()
ret = ret.substr("function ".length)
ret = ret.substr(0, ret.indexOf("("))
return ret
} _innerRun(func, next) {
// console.info('this', this)
// console.info('func', func)
console.info(
`%cfuncName: ${func.name ? func.name : ""}-${this.getFuncName(
func,
)}-ref:${this.arr[this.cIndex].ref}`,
"color:green",
)
this.next = next
if (func.length === 0) {
func()
}
if (func.length === 1) {
func(next)
}
if (func.length === 2) {
func(this.ctx, next)
}
} run(lastCallBack) {
// 插入最后回调执行,但是不影响 数组的内容,避免反复执行会重复插入
if (lastCallBack && this.arr.length > 0) {
const lastIndex = this.arr.length - 1
this.arr[lastIndex].next = lastCallBack
}
if (this.arr.length > 0) {
this._innerRun(this.arr[0].func, this.arr[0].next)
// console.info('asyncTool func.length', this.arr[0].func.length)
}
}
} export default asyncTool

[VueJsDev] 基础知识 - asyncTool.js异步执行工具的更多相关文章

  1. 浅析JS异步执行机制

    前言 JS异步执行机制具有非常重要的地位,尤其体现在回调函数和事件等方面.本文将针对JS异步执行机制进行一个简单的分析. 从一份代码讲起 下面是两个经典的JS定时执行函数,这两个函数的区别相信对JS有 ...

  2. js 异步执行顺序

    参考文章: js 异步执行顺序   1.js的执行顺序,先同步后异步 2.异步中任务队列的执行顺序: 先微任务microtask队列,再宏任务macrotask队列 3.调用Promise 中的res ...

  3. js异步执行 按需加载 三种方式

    js异步执行 按需加载 三种方式 第一种:函数引用 将所需加载方法放在匿名函数中传入 //第一种 函数引用 function loadScript(url,callback){ //创建一个js va ...

  4. JS异步执行之setTimeout 0的妙用

    最近在工作中遇到一些问题,大致是关于js执行问题的.由于没搞清执行顺序,导致出现了一些奇怪的bug. 所以这里整理一些有关异步执行的知识(冰山一角角)... 大家都知道js是单线程的,执行起来是顺序的 ...

  5. 前端知识总结--js异步事件顺序

    js中异步事件中容易混淆的 Promise 和 setTimeout 的执行顺序是怎样的? setTimeout(() => console.log(1), 0); new Promise(fu ...

  6. js异步执行原理

    我们都知道js是一个单线程的语言,所以没办法同时执行俩个进程.所以我们就会用到异步. 异步的形式有哪些那,es5的回调函数.es6的promis等 异步的运行原理我们可以先看下面这段代码 应该很多人都 ...

  7. Java基础知识强化92:日期工具类的编写和测试案例

    1. DateUtil.java,代码如下: package cn.itcast_04; import java.text.ParseException; import java.text.Simpl ...

  8. Java基础知识强化63:Arrays工具类之方法源码解析

    1. Arrays工具类的sort方法: public static void sort(int[] a): 底层是快速排序,知道就可以了,用空看. 2. Arrays工具类的toString方法底层 ...

  9. Java基础知识强化62:Arrays工具类之概述和使用

    1. Arrays工具类: Arrays这个类包含操作数组(比如排序和查找)的各种方法. 2. Arrays的方法: (1)toString方法:把数组转成字符串 public static Stri ...

  10. Android学习之基础知识三(Android日志工具Log的使用)

    Android中的日志工具Log(android.util.Log): 1.打印日志的方法(按级别从低到高排序): Log.v():级别verbose,用于打印最为烦琐,意义最小的日志 Log.d() ...

随机推荐

  1. 解决:VScode中 import 后出现no module的问题

    问题: ModuleNotFoundError: No module named 'xxx' 除去没有安装包的问题 这个问题还是挺难受的,pycharm和终端都可以运行,只有vscode报错 方法一: ...

  2. C# 中的函数与方法

    在C#中,函数和方法都是一段可重用的代码块,用于实现特定的功能.函数是C#中的基本代码块之一,用于完成特定的任务和返回一个值.函数可以具有零个或多个参数,并且可以使用关键字来指定函数的访问级别和返回类 ...

  3. 多路转接高性能IO服务器|select|poll|epoll|模型详细实现

    前言 那么这里博主先安利一下一些干货满满的专栏啦! Linux专栏https://blog.csdn.net/yu_cblog/category_11786077.html?spm=1001.2014 ...

  4. delphi中 注意一点,record 类型 参数默认是 值拷贝,class 参数 默认是传地址;值传递,指针传递、引用传递

    作为函数的入参,若是record类型,默认是值拷贝,效率低,若要传指针,需要加 var ; 作为函数的入参,若是 class类型,默认是传地址,不需要加var unit Unit1; interfac ...

  5. Apache Hudi 设计与架构解读

    1. 简介 Apache Hudi(简称:Hudi)允许您在现有的hadoop兼容存储之上存储大量数据,同时提供两种原语,使得除了经典的批处理之外,还可以在数据湖上进行流处理. 这两种原语分别是: 1 ...

  6. SQL中Between And的问题

    使用Between And时需要注意的事项:(1)"between and"是包括边界值的,"not between"不包括边界值.(2)"betwe ...

  7. JS Leetcode 496. 下一个更大元素 I 更清晰的图解单调栈做法

    壹 ❀ 引 最近一周的工作压力很大...一周的时间一直在处理一个APP漏洞问题,因为项目三年无人维护,突然要改东西光是修改构建错误以及三方包依赖错误就花了三天时间= =.不过好在问题到已经结束尾,闲下 ...

  8. NC16498 [NOIP2014]寻找道路

    题目链接 题目 题目描述 在有向图G中,每条边的长度均为1,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1.路径上的所有点的出边所指向的点都直接或间接与终点连通. 2. ...

  9. RocketMQ—RocketMQ消息重复消费问题

    RocketMQ-RocketMQ消息重复消费问题 重复消费问题的描述 什么情况下会发生重复消费的问题: 生产者多次投递消息:如果生产者发送消息时,连接有延迟,MQ还没收到消息,生产者又发送了一次消息 ...

  10. 【OpenGL ES】绘制三角形

    1 前言 1.1 设置顶点属性 ​ 顶点有位置.颜色等属性,可以通过 glVertexAttribPointer 设置顶点的属性. void glVertexAttribPointer( int in ...