AxiosCanceler.js 740 B

12345678910111213141516171819202122232425262728293031
  1. export default class AxiosCanceler {
  2. constructor() {
  3. this.pendingMap = new Map
  4. }
  5. addPending(e) {
  6. return new axios.CancelToken(t=>{
  7. this.pendingMap.has(e) || this.pendingMap.set(e, t)
  8. }
  9. )
  10. }
  11. removeAllPending() {
  12. this.pendingMap.forEach(e=>{
  13. e && isFunction(e) && e()
  14. }
  15. ),
  16. this.pendingMap.clear()
  17. }
  18. removePending(e) {
  19. if (this.pendingMap.has(e)) {
  20. const t = this.pendingMap.get(e);
  21. t && t(e),
  22. this.pendingMap.delete(e)
  23. }
  24. }
  25. removeCancelToken(e) {
  26. this.pendingMap.has(e) && this.pendingMap.delete(e)
  27. }
  28. reset() {
  29. this.pendingMap = new Map
  30. }
  31. }