取消请求实战

关于前端取消请求,大多数文章都是new AbortController() 然后将signal放到请求中,然后执行abort取消,这种文章怎么好意思拿出来啊,mmmmini版的官方文档罢了。

首先要明确我们想要的效果是什么,可以对比一下toast.showloading(),当我们发起一个请求打开loading时,什么时候关闭是由我们随时随地决定的。随时就不解释了,随地就是我在组件中打开了loading,我可以其他任何组件中关闭这个loading。

话不多说,看我操作

首先封装一个全局公共类

export default class CancelManager {
  cancelMap
  static instance

  constructor() {
    this.cancelMap = new Map()
    this.cancelKey = []
  }

  static getInstance() {
    if (!CancelManager.instance) {
      CancelManager.instance = new CancelManager()
    }
    return CancelManager.instance
  }

  has(key) {
    return this.cancelMap.has(key)
  }

  hasCancel(key) {
    return this.cancelKey.includes(key)
  }

  getSignal(key) {
    if (!key) {
      console.log('getSignal的key不能为空')
      return
    }

    const controller = new AbortController()
    const { signal } = controller

    this.cancelMap.set(key, controller)

    return signal
  }

  cancel(key) {
    const task = this.cancelMap.get(key)

    if (!task) {
      console.log(key + '控制器不存在')
      return
    }

    task.abort()
    this.delete(key)
    this.cancelKey.push(key)
    console.log(key, '请求已取消,cancelKey=>>', this.cancelKey)
  }

  cancelAll() {
    this.cancelMap.forEach((task) => {
      task.abort()
    })
    this.cancelKey = []
    this.cancelMap.clear()
  }

  delCancelKey(key) {
    this.cancelKey = this.cancelKey.filter((v) => v !== key)
  }

  delete(key) {
    this.cancelMap.delete(key)
  }

  clear() {
    this.cancelMap.clear()
  }
}

使用

 //这里的id也可是是这个函数,因为里面是用的Map存储,保证唯一性即可,以及方便在其他地方能拿到这个id
 const controller = CancelManager.getInstance()//单例模式随处get都行
 const signal = controller.getSignal(id)
 
 //此处只需将signal传给axios等请求库就行了
 const res = await getData(params, { signal })

取消

 const controller = CancelManager.getInstance()
 controller.cancel(id)

相关推荐

  1. 取消请求实战

    2024-04-10 19:32:02       44 阅读
  2. s AbortController 接口取消多次请求 取消上次请求

    2024-04-10 19:32:02       31 阅读
  3. 竞态问题 + axios 取消请求

    2024-04-10 19:32:02       125 阅读
  4. Vue路由切换 & Axios接口取消重复请求

    2024-04-10 19:32:02       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 19:32:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 19:32:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 19:32:02       82 阅读
  4. Python语言-面向对象

    2024-04-10 19:32:02       91 阅读

热门阅读

  1. golang主要优缺点

    2024-04-10 19:32:02       43 阅读
  2. 函数参数的类型

    2024-04-10 19:32:02       43 阅读
  3. flutter ios 运行报错

    2024-04-10 19:32:02       41 阅读
  4. 用选择法对数组中10个整数按由小到大排序

    2024-04-10 19:32:02       41 阅读
  5. 如何在苹果手机上安装iOS应用的.ipa文件?

    2024-04-10 19:32:02       116 阅读
  6. 每天学习一个Linux命令之hostnamectl

    2024-04-10 19:32:02       38 阅读
  7. 大语言模型RAG vs. 长文本

    2024-04-10 19:32:02       36 阅读
  8. 自然语言处理(NLP)技术

    2024-04-10 19:32:02       34 阅读
  9. 探索ChatGPT应用:学术写作实践与经验分享

    2024-04-10 19:32:02       29 阅读