码迷,mamicode.com
首页 > 其他好文 > 详细

前端排队请求

时间:2018-06-01 00:59:26      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:res   turn   sync   dialog   err   dia   request   ota   ogr   

enum ShowStopDialogRes {
  cancle,
  sure,
}
interface Counter {
  total: number;
  ended: number;
  error: number;
  success: number;
}
interface CodeObj<T> {
  Data: T;
  Msg: string | null;
  Code: number;
}
class QueueRequest {
  hasStopDialog: boolean;
  isShow: boolean;
  isStop: boolean;
  reqs = [] as any[];
  counter = {
    total: 0,
    ended: 0,
    error: 0,
    success: 0,
  } as Counter;

  init() {
    this.send(0);
   
  }

  @watch(‘counter.ended‘)
  endedChange (ended: number) {
    if (ended === this.counter.total) {
      this.quite();
    }
  }

  constructor () {
    this.init();
  }

  async cancle () {
    if (this.isStop) {
      this.isShow = true;
      return;
    }
    this.hasStopDialog = true;
    const res = await this.showStopDialog();
    this.hasStopDialog = false;
    // 取消终止
    if (res === ShowStopDialogRes.sure) {
      this.isStop = false;
      if (this.counter.ended === this.counter.total) {
        this.counter.total = this.reqs.length;
      }
      return;
    }
    // 确定终止
    if (this.counter.total === this.counter.total) {
      this.quite();
    }
  }

  stop () {
    // todo
  }
  quite () {
    this.resetAndDectory();
  }

  async send (index: number) {
    if (this.isStop) return;

    const req = this.reqs[index];
    if (req) return;

    const res = await this.fetchApi(req);

    if (res.Code !== 200) {
      alert(‘bug,还不修!‘);
    }

    if (this.isStop) return;

    this.send(++index);
  }

  async fetchApi (req: any): Promise<CodeObj<null>> {
    let res: Promise<CodeObj<null>>;
    // todo
    return res;
  }
  // 暂停弹窗
  async showStopDialog () {
    let ret: ShowStopDialogRes;
    // todo
    return ret;
  }
  // 重置并销毁
  resetAndDectory () {
    // todo
  }
}

前端排队请求

标签:res   turn   sync   dialog   err   dia   request   ota   ogr   

原文地址:https://www.cnblogs.com/liangcheng11/p/9119805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!